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?

Community
  • 1
  • 1
  • 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. – jogojapan Oct 18 '12 at 05:51
  • 1
    Why wouldn't it be? Pointers are a type, just like any other. – chris Oct 18 '12 at 05: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. – Skandh Oct 18 '12 at 05: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. – Benjamin Lindley Oct 18 '12 at 05:56
  • They should be public.... I made the changes. –  Oct 18 '12 at 06:05

4 Answers4

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

Vijay
  • 65,327
  • 90
  • 227
  • 319
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;
}
Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
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.

AndersK
  • 35,813
  • 6
  • 60
  • 86
-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

khan
  • 2,664
  • 8
  • 38
  • 64
  • 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. – jogojapan Oct 18 '12 at 06:04
  • but i have mentioned that if we assign them then this line must be true b->a->foo() – khan Oct 18 '12 at 06:08
  • 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. – jogojapan Oct 18 '12 at 06:13
  • 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()`... – jogojapan Oct 18 '12 at 06:16
  • 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. – Alnitak Oct 18 '12 at 06:16
  • @Alnitak True, but both the title of the question as well as the linked question talk about pointers to pointers. – jogojapan Oct 18 '12 at 06:17