About incomplete class definition

C

CC Jia

Hi, guys, I'm a cpp newbie.
I have a question about class definition, here is:
class C{
public:
C test2(){std::cout<<"test2\n"; return *this;}
void print();
C testDefinition();
};

As i know, in class definition, it is incomplete, why can I make
return a object of this class?
 
C

CC Jia

Hi, guys, I'm a cpp newbie.
I have a question about class definition, here is:
class C{
public:
C test2(){std::cout<<"test2\n"; return *this;}
void print();
C testDefinition();
};
As i know, in class definition, it is incomplete, why can I make
return a object of this class?

For testDefinition(), this is only a declaration, which is OK with
incomplete types.

For test2(): from C++ standard 9.2/2: "Within the class member-
specification, the class is regarded as complete within function bodies,
[...]"

It seems the aim has been that when defining member functions inside the
class definition one should not encounter strange restrictions, the
compiler is required to handle them in the same way as if they were
defined outside of the class definition (and marked "inline" explicitly).

hth
Paavo

yes, thank you, your reply is clear, i can understand
 
J

Johannes Schaub (litb)

CC said:
Hi, guys, I'm a cpp newbie.
I have a question about class definition, here is:
class C{
public:
C test2(){std::cout<<"test2\n"; return *this;}
void print();
C testDefinition();
};

As i know, in class definition, it is incomplete, why can I make
return a object of this class?

For test2, it's helpful to consult the exact rule: "The type of a parameter
or the return type for a function definition shall not be an incomplete
class type (possibly cv-qualified) unless the function definition is nested
within the member-specification for that class (including definitions in
nested classes defined within the class)."
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top