Jump to content
  • Advertisement
Sign in to follow this  
Toni Petrina

What does this do?

This topic is 4964 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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 MyNum
public:
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Quote:
Original post by Genjix

genjix@linux:~/tmp> g++ main.cpp
main.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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.cpp
main.cpp: In function `int main()':
main.cpp:5: error: declaration does not declare anything


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!