CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Nov 2010
    Location
    CA
    Posts
    29

    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?

  2. #2
    Join Date
    Jan 2009
    Posts
    1,689

    Re: C++ style assignment not working

    That's not assignment syntax, that's initialization syntax

  3. #3
    Join Date
    Nov 2010
    Location
    CA
    Posts
    29

    Re: C++ style assignment not working

    Quote Originally Posted by ninja9578 View Post
    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.

  4. #4
    Join Date
    Nov 2010
    Location
    CA
    Posts
    29

    Post 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.

  5. #5
    Join Date
    Jan 2009
    Posts
    1,689

    Re: C++ style assignment not working

    Theoretically, they should degenerate into the same code, but no guarantees.

  6. #6
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    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

  7. #7
    Join Date
    Jan 2009
    Posts
    1,689

    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:

    Code:
    int i = 0;
    I usually see the other style of initialization only with objects, or initialization lists in ctors.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: C++ style assignment not working

    Quote Originally Posted by ninja9578 View Post
    I don't believe it is very common to see that syntax for primitives. I almost always see code like this:

    Code:
    int i = 0;
    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
  •  





Click Here to Expand Forum to Full Width

Featured