Thread: Initializing the class member in class function gives me wrong output

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    145

    Initializing the class member in class function gives me wrong output

    why does the output of value of b = 1 ?

    I have below code:
    Code:
    class test
    {
    	int a;
    	public:
    	int b;
    	int geta();
    };
    
    int test::geta()
    {
       a=100;	b=200; return a;
    }
    
    int main()
    {
    	test obj;
    	cout<< "value of a:" <<obj.geta() <<" value of b:" <<obj.b <<'\n';
    	return 0;
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,720
    It doesn't... You must be setting it someplace else, not shown here.
    Devoted my life to programming...

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,014
    The order of evaluation of function arguments is unspecified, so you cannot depend on obj.geta() being evaluated before obj.b, since a possible order of evaluation exists such that obj.b is evaluated before obj.geta().

    You should have declared geta to be const, though then you wouldn't be able to modify a or b unless you declared them mutable.

    EDIT:
    Oh, I notice that you didn't even initialise b to be 1... so either GReaper's guess is correct, or you're looking at the result of printing an uninitialised variable. Did you really test the code that you posted? You left out the header inclusions, and cout is within the std namespace.
    Last edited by laserlight; 04-02-2019 at 03:42 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    Thanks for the replies...


    I have not mentioned the headers here , though I have them while executing .No initializations to 1 were made.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,014
    Well, for what it's worth, what you should have done was this:
    Code:
    class test
    {
    public:
        int b = 200;
    
        int geta() const
        {
            return a;
        }
    private:
        int a = 100;
    };
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 01-06-2015, 09:04 AM
  2. Member function from class as a friend in another class
    By thames in forum C++ Programming
    Replies: 18
    Last Post: 01-02-2013, 05:32 AM
  3. Initializing constant object member of a class
    By Canadian0469 in forum C++ Programming
    Replies: 3
    Last Post: 12-03-2008, 08:05 PM
  4. Replies: 8
    Last Post: 03-19-2008, 03:04 AM
  5. Getting wrong output from a class
    By orikon in forum C++ Programming
    Replies: 11
    Last Post: 11-18-2005, 07:58 PM

Tags for this Thread