0

Are pointers to pointers legal in c++? I've come across this SO question: Pointer to Pointer to Pointer

But the answers aren't clear if it is legal c++ or not. Let's say I have:

class A{
public:
    void foo(){
        /* ect */
    }
};

class B{
public:
    A* a;
    /* ect */
};

void Some_Func() {
    B *b;

    // besides this looking ugly, is it legal c++?
    b->a->foo();
};

Is the line b->a->foo() OK to write? Is there a better way to represent this expression?

5
  • 1
    Apart from the fact that foo and a are private in each of their classes (you'd have to make them public for this to work), why should it not be ok? It's not even particularly ugly, actually. Commented Oct 18, 2012 at 5:51
  • 1
    Why wouldn't it be? Pointers are a type, just like any other. Commented Oct 18, 2012 at 5:52
  • 1
    They are perfectly ok, but you cannot access 'a' from 'b' (b->a) directly as a is private member of class B. Otherwise it should work. Commented Oct 18, 2012 at 5:53
  • 2
    What you have here is not a pointer to a pointer. It is a pointer to an object that has a pointer member. Commented Oct 18, 2012 at 5:56
  • They should be public.... I made the changes. Commented Oct 18, 2012 at 6:05

4 Answers 4

2

This is perfectly valid.But the term you are using "pointer to pointer" is wrong.

the term means a double pointer like **P,a pointer which holds the address of another pointer.

but your case is the pointer(of class A) is an member of a class whose pointer(of class B) is created by you in some_func

Sign up to request clarification or add additional context in comments.

2 Comments

"b" is a pointer of class B, which holds a pointer to class A.
Thanks for correcting my use of the term 'pointer to a pointer'.
0

It is legal but in your example your program will crash (if you could compile it since your members are private) because you did not create an instance of B

void Some_Func() {
    B *b;  // uninitialized B ptr  should be   B* b = new B;

    // legal
    b->a->foo();
};

Although you may want to reconsider accessing variables directly as you do and instead have getter/setters to access private member variables.

Comments

0

Illegal, "a" is private. So is "foo".

If corrected to "public" then they're legal constructs.

From your code its hard to find a "better" way. BUT You can modify the code to make the code look much clearer:

class A{
public:
    void foo(){ cout << "Work";}
};

class B{
private:
    A *a;
public:
    A& getA(){
       return *a;
    }
};

void SomeFunction()
{
    B *b = new B();
    B& bRef = *b;
    bRef.getA().foo(); //better looking code? 
        delete b;
}

Comments

-1

Yes but then you have to use pointer to pointer like **P.
Actually if we want to access a pointer which is holding another pointer then we can do this.This is allowed in c++ but keep in mind that only in case if you have assigned a pointer to pointer p

6 Comments

What do you mean "only in case of public"? What you describe in the answer is a pointer to pointer, but for that you don't need the class A and class B construction originally proposed by the questioner.
but i have mentioned that if we assign them then this line must be true b->a->foo()
If you are still talking about the original example, you need a pointer A **a (not sure what you mean by P in that case), and the way you call foo is (**a).foo() or (*a)->foo() then.
No, I mean, of course you call the variable p, but for a start, in your text you have uppercase P and lowercase p, while in the comment you talked about b->a->foo()...
there are no "pointers to pointers" in this question. Instances of class B contain a pointer to an instance of another class, but there are no ** type variables involved.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.