Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6012385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:23:48+00:00 2026-05-23T02:23:48+00:00

I have a class that should have a private member of the same class,

  • 0

I have a class that should have a private member of the same class, something like:

class A {
    private:
        A member;
}

But it tells me that member is an incomplete type. Why? It doesn’t tell me incomplete type if I use a pointer, but I’d rather not use a pointer. Any help is appreciated

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:23:49+00:00Added an answer on May 23, 2026 at 2:23 am

    At the time you declare your member, you are still defining the A class, so the type A is still undefined.

    However, when you write A*, the compiler already knows that A stands for a class name, and so the type “pointer to A” is defined. That’s why you can embed a pointer to the type your are defining.

    The same logic applies also for other types, so if you just write:

    class Foo;
    

    You declare the class Foo, but you never define it. You can write:

    Foo* foo;
    

    But not:

    Foo foo;
    

    On another hand, what memory structure would you expect for your type A if the compiler allowed a recursive definition ?

    However, its sometimes logically valid to have a type that somehow refer to another instance of the same type. People usually use pointers for that or even better: smart pointers (like boost::shared_ptr) to avoid having to deal with manual deletion.

    Something like:

    class A
    {
      private:
        boost::shared_ptr<A> member;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a base class that has a private static member: class Base {
I have a helper class that should only ever run in a background thread.
Scala programmer should have known that this sort of writing : class Person{ var
I have class that looks like this: class A { public: class variables_map vm
I have a Class that has a private variable with a public setter/getter function:
I have a class that has several member classes as attributes. The constructor of
Suppose I have a class like this: class MyClass { private: vector<MyOtherClass> myMember; public:
I have a singleton class for which I need a private member. I want
I have a signal and a slot that should fit together quite nicely. class
I have class that extend FragmentActivity in it I add fragment to layout as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.