-5

RESOLVED
Sorry for all the confusion - I'll try harder to ask better questions in the future!

My original question was basically whether or not it was possible to create an instance of a class before the class was defined.

I've looked around a lot for this and I haven't really found a solution.
Basically, I want to be able to do this:

namespace a {
    class first
    {
    private:
        second s; //Throws an error
    }
    class second
    {
    private:
        first f; //Seems to work
    }
}

But it doesn't work. Even when I specify a::second, it doesn't recognize them.

So is it possible to access members of the same namespace before they are declared in that namespace? If so, how?

I've since figured it out, and the answer is that no, you cannot create instances of a class before it's declared, but you can forward-declare the class and create a pointer to it.

arvb
  • 13
  • 5

3 Answers3

0

So is it possible to access members of the same namespace before they are declared in that namespace? - No, everything you use must be declared first:

namespace a {
    // here you declare class second, but it's an incomplete class
    // until it's defined below
    class second;

    class first
    {
    private: 
        // Note that you can't write "second s;"
        // because second would have to be a complete class in this case
        second* s;
        // or you can use a smart pointer like std::shared_ptr<second>
    };

    class second
    {
    private:
        first f;
    };
}
Anton Savin
  • 40,838
  • 8
  • 54
  • 90
0

You can use forward declaration of your class second and use a pointer. Only the implementation must know the declaration of your class Second.

namespace A {
    class Second;  // Forward declaration
    class First
    {
    public:
        First();
    ~First();
    private:
        Second* s; // Pointer on a forward class
    };

    class Second
    {
    private:
        First f;
    };


    First::~First()
    {
        delete s;
    }

    First::First()
    {
        s = new Second();
    }

}
JPLemelin
  • 340
  • 4
  • 10
  • For more information about when to use forward delcaration: http://stackoverflow.com/questions/553682/when-can-i-use-a-forward-declaration – JPLemelin Nov 16 '14 at 18:27
-1

You should understand that you can't access a::second because it is private; and by default everything is private within a class when no access specifier is named. If you want to access an element outside the class, then you should make the members public. Don't forget the semicolon after each class' end-brace.

Poriferous
  • 1,566
  • 4
  • 20
  • 33
  • _"...you can't access a::second because it is private"_ - only elements at class scope can be `public`, `protected`, `private`. `second` is declared at namespace scope so access specifiers have no application here. – Captain Obvlious Nov 16 '14 at 19:44
  • I think we misunderstood the question. I'm reading in class first that 'second s' is in private scope and therefore not accessible. The class thereof is declared in namespace scope; therefore to resolve any issues regarding declaration, a forward declaration of the class 'second' should be instated. – Poriferous Nov 16 '14 at 20:08
  • The question has nothing to do with whether `s` is private or not but whether `Second` is visible when `s` is declared. Regardless, `a::Second` is not private and can't be private so the first part of your answer is wrong and misleading. – Captain Obvlious Nov 16 '14 at 20:27
  • The question is misleading; but your answer is also misleading. I'll abstain for this one! – Poriferous Nov 16 '14 at 23:10