i believe here that static member type const double can't have in calss initializer...correct me if am wrong? so i feel answer is a?
Printable View
static const double d = 2.0; isn't valid C++. it shouldn't compile at all. what do you think guys?
Quit guessing! Look at your options.
a) x
b) d
c) x and d
d) none
Re-read and answer the question from my last post.
Jim
Read your error message.
Why do you think that static const double d = 2.0; isn't valid? d = 2.0 is a constant expression so it's allowed in the class definition.
[edit: forgot that this is a g++ extension. Only integral values allowed in ISO C++]
What you can't do in the class definition is to use a function call to initialize a member, as in static const int B::x = fn();. But you can initialize that data member outside the class definition like this:
Code:int fn () {return 5;}
class B {
private:
static const int x;
static const double d = 2.0;
};
const int B::x = fn();
int main ()
{
}
If I google "static const double initializer", the first hit is this: c++ - Why can't I have a non-integral static const member in a class? - Stack Overflow
You should try searching for answers to questions that you have.
If you're using an online compiler like Comeau you can get compiler error messages, but you won't be able to run the resulting code, which is really a barrier to learning. You can download environments like Code::Blocks or Cygwin or MinGW that provide the gcc compiler on Windows, if you're more familiar with gcc from Linux.
so do you mean answer is "none" here?
What do you think and why?
Jim
i am not sure jim, please advice me to find correct answer here.
Your choices are:
a) x
b) d
c) x and d
d) none
Of these four choices what do you think is the answer? Remember this is your error message!
while running with online compiler, i got below error...pls help
Line 1: error: 'int fn()' cannot appear in a constant-expression
compilation terminated due to -Wfatal-errors.
Then answer this question I asked in a previous post!
If you get an error message can a, b, or c ever happen? Remember when you get an error message your program will not run, therefor it produces no output.
The answer to your question should be obvious by now! This is not a trick question. If you want to be a programmer you need to learn to think and reason.
Jim