Jump to content

  • Log In with Google      Sign In   
  • Create Account


[C++] Question about Variable Declarations


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 NightCreature83   Crossbones+   -  Reputation: 2674

Like
0Likes
Like

Posted 31 May 2012 - 02:48 AM

This is not really a code problem I am having more of a why isn't this allowed by the C++ standard, I googled a bit but couldn't find a solution and would like to have an answer to the question.

Say we have this piece of sample code:

class SimpleTestObject
{
public:
	SimpleTestObject() : a(0) {}
	SimpleTestObject(int a) : a(a) {}
	void print() { std::cout << a << std::endl; }
private:
	int a;
};
int _tmain(int argc, _TCHAR* argv[])
{
	SimpleTestObject obj();
	obj.print();
return 0;
}

We get a "error C2228: left of '.print' must have class/struct/union" and to remove this error I need to remove the "()" on the line above. However if I pass a number in this constructor it compiles without complaining.
My question is why am I not allowed to called the default constructor explicitly, and more over why does the compiler think I am defining a function in a function?
Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

Sponsor:

#2 wqking   Members   -  Reputation: 756

Like
0Likes
Like

Posted 31 May 2012 - 02:58 AM

If it's allowed, how can you declare a function with no parameters and return an object of SimpleTestObject?

When never you write,
SimpleTestObject ThisIsAFunction();

The compiler has to treat it as an object.
Then there is no way you can write a function like that.

Edited by wqking, 31 May 2012 - 02:59 AM.

http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.


#3 rip-off   Moderators   -  Reputation: 7723

Like
0Likes
Like

Posted 31 May 2012 - 04:20 AM

...

why does the compiler think I am defining a function in a function?


Because according to the syntax rules of C++, that is (almost) what you are doing. To be accurate, what the compiler thinks is that you are declaring a function called "obj" which takes no parameters and returns a SimpleTestObject.

See also C++'s most vexing parse.

#4 NightCreature83   Crossbones+   -  Reputation: 2674

Like
0Likes
Like

Posted 31 May 2012 - 05:08 AM


...

why does the compiler think I am defining a function in a function?


Because according to the syntax rules of C++, that is (almost) what you are doing. To be accurate, what the compiler thinks is that you are declaring a function called "obj" which takes no parameters and returns a SimpleTestObject.

See also C++'s most vexing parse.


Yeah I have seen that link before must have forgotten about it, cheers though
Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS