-
November 27th, 2010, 10:18 PM
#1
C++ style assignment not working
I read that C++ has a new type of assignment syntax that I can choose to utilize.
I.e.
Code:
int a(10); // C++ style
int a = 10; // C stlye
Sometimes this syntax fails, such as below:
Code:
class CDisplay {
public:
CDisplay();
virtual ~CDisplay();
;
void TimeStamp(void);
void Message(const std::string& Message);
void Message(const std::string& Text, const std::string& Message);
void SetLogLevel(bool value) {
bLogLevel = value;
// If I change the above line to: bLogLevel(value); the compiler says:
// CDisplay.h|24|error: '((CDisplay*)this)->CDisplay::bLogLevel' cannot be used as a function
}
bool GetLogLevel(void) {
return bLogLevel;
}
protected:
private:
bool bLogLevel;
};
Now it thinks the C++ style is a function..
How do I know when it's safe to use this C++ style of assignment? Should I just abandon it for C style assignment?
-
November 27th, 2010, 10:27 PM
#2
Re: C++ style assignment not working
That's not assignment syntax, that's initialization syntax
-
November 27th, 2010, 10:40 PM
#3
Re: C++ style assignment not working
Originally Posted by ninja9578
That's not assignment syntax, that's initialization syntax
I apologize, I'm still trying to learn the correct terms for things. I still don't understand why it works in some places, but not others. My book doesn't seem to explain why, it just says it can be used in the places I would normally use C assignment.
It says that a=10 is the same as a(10)
It appears this isn't always the case.
-
November 27th, 2010, 10:52 PM
#4
Re: C++ style assignment not working
I made a little program to try to understand
Code:
int main() {
int a = 10;
a(20); // produces same error
}
So I guess the big clue was the word initialization. Once a variable exists, it can't exactly be initialized again. Makes sense to me now.
-
November 28th, 2010, 12:24 AM
#5
Re: C++ style assignment not working
Theoretically, they should degenerate into the same code, but no guarantees.
-
November 29th, 2010, 07:57 AM
#6
Re: C++ style assignment not working
Code:
int a = 20; // CREATES an integer named "a" , and initializes it to 20
int a(20); // CREATES an integer named "a" , and initializes it to 20
a(20); // calls a function named "a" which takes one parameter
-
November 29th, 2010, 03:18 PM
#7
Re: C++ style assignment not working
I don't believe it is very common to see that syntax for primitives. I almost always see code like this:
I usually see the other style of initialization only with objects, or initialization lists in ctors.
-
November 29th, 2010, 07:50 PM
#8
Re: C++ style assignment not working
Originally Posted by ninja9578
I don't believe it is very common to see that syntax for primitives. I almost always see code like this:
I usually see the other style of initialization only with objects, or initialization lists in ctors.
In templated code, where the template type can be anything (primitive, a class/struct, etc.), then you will see this syntax used.
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|