What does this do?

Started by
8 comments, last by load_bitmap_file 19 years, 1 month ago
class CTest
{
  public:
    void Hello()
    {
      std::endl << "Hello" <<std::endl;
    }
}

// both lines compile simultaneously
CTest().Hello();    //<-  ???
CTest();
What happens in that line? Is unnamed object created(I think not since compilation would fail)? What's the point?
So... Muira Yoshimoto sliced off his head, walked 8 miles, and defeated a Mongolian horde... by beating them with his head?

Documentation? "We are writing games, we don't have to document anything".
Advertisement
Quote:Original post by ffx
Is unnamed object created(I think not since compilation would fail)?

Why do you think the compilation would fail? There's no reason that you can't have an unnamed temporary on the stack or heap. You can use it quite a lot, for example SomeFunc(Vector(1,2,3)); where Vector is a 3d vector class. There's no named vector created there, just one on the stack that gets passed into the function. What that line does is create an unnamed object of type CTest, calls Hello on it, then destroys it.

This is completely legal:
class SomeClass{	static int CurNum;	int MyNumpublic:    SomeClass()    {		MyNum = ++CurNum;		std::cout << "Created " << CurNum << std::endl;    }    ~SomeClass()    {		std::cout << "Destroyed " << MyNum << std::endl;    }};int SomeClass::CurNum = 0;void SomeFunc(const SomeClass&)//unnamed parameter{	SomeClass();// Unnamed temporary	new SomeClass;// unnamed on heap (note: this is bad.)	class //unnamed class type	{	public:		Write()//no return type--MSVC2005 assumes int.		{			std::cout << "Unnamed write" << std::endl;		}//no return, returns junk on stack.	} Temp;//though needs name down here or is useless.	Temp.Write();}int main(){	SomeFunc(SomeClass());//Unnamed temporary	return 0;}

And it prints:
Created 1
Created 2
Destroyed 2
Created 3
Unnamed write
Destroyed 1

[Edited by - Erzengeldeslichtes on March 14, 2005 5:50:35 AM]
----Erzengel des Lichtes光の大天使Archangel of LightEverything has a use. You must know that use, and when to properly use the effects.♀≈♂?
Quote:Original post by Erzengeldeslichtes
Quote:Original post by ffx
Is unnamed object created(I think not since compilation would fail)?

Why do you think the compilation will fail? There's no reason that you can't have an unnamed temporary on the stack or heap. You can use it quite a lot, for example SomeFunc(Vector(1,2,3)); where Vector is a 3d vector class. There's no named vector created there, just one on the stack that gets passed into the function. What that line does is create an unnamed object of type CTest, calls Hello on it, then destroys it.


I suspected that much. Tnx.
So... Muira Yoshimoto sliced off his head, walked 8 miles, and defeated a Mongolian horde... by beating them with his head?

Documentation? "We are writing games, we don't have to document anything".
In most cases you would make your Hello method static to indicate that it doesn't need to be part of any particular object. Just a member of the CTest class.

Another thing you can do if its static is this.

CTest *pTest = NULL;
pTest->Hello();

SOme compilers will generate code that runs for this as Hello is static the compiler knows it can instantiate a temporary variable here and call just fine.

Cheers
CHris
CheersChris
A bit offtopic but I was surprised the code the OP posted compiled and ran. What happens if you do something like this:

class A{};int main(){    A;    return 0;}


The constructor doesn't get called for A, but I didn't even know this would compile let alone run.
Quote:Original post by load_bitmap_file
A bit offtopic but I was surprised the code the OP posted compiled and ran. What happens if you do something like this:

class A{};int main(){    A;    return 0;}


The constructor doesn't get called for A, but I didn't even know this would compile let alone run.


Yes, the default constructor is called. A default constructor is created and can be called even if you don't define one yourself.
President: Video Game Development Club of UCIhttp://spirit.dos.uci.edu/vgdc
genjix@linux:~/tmp> g++ main.cppmain.cpp: In function `int main()':main.cpp:5: error: declaration does not declare anything

Quote:Original post by Genjix
genjix@linux:~/tmp> g++ main.cppmain.cpp: In function `int main()':main.cpp:5: error: declaration does not declare anything


Ok, could be compiler specific. It may not be allowed standards compliant C++. Visual Studio .NET 2003 gives this:

------ Build started: Project: test, Configuration: Debug Win32 ------

Compiling...
test.cpp
Linking...

Build log was saved at "file://d:\Engineer\delete me\test\Debug\BuildLog.htm"
test - 0 error(s), 0 warning(s)

Program does nothing.
President: Video Game Development Club of UCIhttp://spirit.dos.uci.edu/vgdc
Actually if you don't put the paranthesis in VC it won't actually do anything. In the code I posted, in SomeFunc, if you put SomeClass; instead of SomeClass(); it doesn't do the create-destroy thing. It compiles, but it doesn't generate code. I know this because that's what I originally did when typing that code... but it didn't do anything, so I put on the parenthesis.
----Erzengel des Lichtes光の大天使Archangel of LightEverything has a use. You must know that use, and when to properly use the effects.♀≈♂?
Quote:Original post by Deathscythe
Quote:Original post by load_bitmap_file
A bit offtopic but I was surprised the code the OP posted compiled and ran. What happens if you do something like this:

class A{};int main(){    A;    return 0;}


The constructor doesn't get called for A, but I didn't even know this would compile let alone run.


Yes, the default constructor is called. A default constructor is created and can be called even if you don't define one yourself.


No, the default constructor is not called. Define A's constructor to print a message and you'll see nothing gets outputted. And the implicit constructor is not what I'm talking about.

Quote:Original post by Genjix
genjix@linux:~/tmp> g++ main.cppmain.cpp: In function `int main()':main.cpp:5: error: declaration does not declare anything


I guess it's not standard C++ then.

This topic is closed to new replies.

Advertisement