About incomplete class definition

Discussion in 'C++' started by CC Jia, Oct 30, 2010.

  1. CC Jia

    CC Jia Guest

    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?
     
    CC Jia, Oct 30, 2010
    #1
    1. Advertising

  2. CC Jia

    CC Jia Guest

    On 10ÔÂ30ÈÕ, ÏÂÎç11ʱ18·Ö, Paavo Helde <> wrote:
    > CC Jia <> wrote in news:61ae2f5e-92e9-46cc-be4e-
    > :
    >
    > > 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
     
    CC Jia, Oct 30, 2010
    #2
    1. Advertising

  3. CC Jia wrote:

    > 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)."
     
    Johannes Schaub (litb), Oct 31, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jianli Shen
    Replies:
    1
    Views:
    645
    Victor Bazarov
    Mar 13, 2005
  2. Ark
    Replies:
    1
    Views:
    453
    Chris Torek
    Aug 7, 2004
  3. Jon Slaughter
    Replies:
    4
    Views:
    474
    Jon Slaughter
    Oct 26, 2005
  4. =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki

    Decralation of class inside other class and definition outside this class

    =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki, Jul 13, 2007, in forum: C++
    Replies:
    2
    Views:
    359
    Alf P. Steinbach
    Jul 13, 2007
  5. Pierre Yves
    Replies:
    2
    Views:
    526
    Pierre Yves
    Jan 10, 2008
Loading...

Share This Page