No identifier required?

M

marbac

Hi,

i was just testing a class within main and was wondering why this works :


#include <iostream>

class testclass {
public:
testclass (int _test) : test(_test){};
private:
int test;
};


int main () {
testclass (8);
std::cout<<"Seems to work?"<<std::endl;
return 0;
}


My Question: What does "testclass (8);" do? (works without compilation
error with g++ (gcc) (3.3.3), stdout:"Seems to work?" ).

regards marbac
 
A

Andrey Tarasevich

marbac said:
...
int main () {
testclass (8);
std::cout<<"Seems to work?"<<std::endl;
return 0;
}


My Question: What does "testclass (8);" do? (works without compilation
error with g++ (gcc) (3.3.3), stdout:"Seems to work?" ).
...

'testclass (8)' is an expression statement that creates a temporary
object of type 'testclass'. The object is then immediately destroyed.
That's all.
 
M

Mike Wahler

marbac said:
Hi,

i was just testing a class within main and was wondering why this works :


#include <iostream>

class testclass {
public:
testclass (int _test) : test(_test){};
private:
int test;
};


int main () {
testclass (8);
std::cout<<"Seems to work?"<<std::endl;
return 0;
}


My Question: What does "testclass (8);" do?

Creates a type 'testclass' object, then immediately
destroys it.
error with g++ (gcc) (3.3.3), stdout:"Seems to work?" ).

It works. It's well-defined.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,649
Latest member
MargaretCo

Latest Threads

Top