0

Below is my code

Class A
{
  A::A(int num) { }
  int num;
};

class B : public A
{
  B::B(int num):A(num) { }
};

Class D;
Class C
{
  void getNum(A**& somenum) {}
  D *dObj;
};

void C::getNum(A**& somenum)
{
  dObj->getNumber(static_cast<B**>(somenum)); // Error here.
}

Class D
{
  void getNumber(B**& number) 
  { 
      B someValue[5];
      // all the objects in the array are properly created and properly Initalized (skipped that part)

      number[0] = someValue[0]; 
      number[1] = someValue[1];
      //...
   }
};

I'm getting compilation error while doing the static_cast. I am trying to assign the values in "someValue" array to "A**& somenum". Can you please help how to do it.

Thank you very much in advance.

Jabez
  • 1,883
  • 6
  • 19
  • 18
  • 1
    Why would you need pointers to pointers passed by reference ever? – Overv Jan 17 '13 at 13:37
  • Can you please post what is the right way to do it. I searched for solution and tried so many other ways but failed. :( As a last resort, i posted the Question here. :(:( – Jabez Jan 17 '13 at 13:38
  • In `B**& number` , number is reference variable for `B**` that pointer to pointer of Class B. – Grijesh Chauhan Jan 17 '13 at 13:39
  • 2
    "Can you please help how to cast the "somenum" so that "someValue" will be assigned to it." Casting is simply the wrong thing to do. In fact, more than 90% of the questions on SO that start "how to cast..." are the wrong question. Tell us what you want to do (and just to be clear, casting is not what you want to do), and you may get some actual help. – R. Martinho Fernandes Jan 17 '13 at 13:44
  • what is type of someValue ? – Neel Basu Jan 17 '13 at 13:51
  • sorry for the confusion. I want to assign the values of "someValue" which is an array of type B to "somenum" which is an array of type A. – Jabez Jan 17 '13 at 13:52
  • why are you making it so complicated? of course you're gonna confuse yourself! There is clearly a type mismatch here "number[0] = someValue[0]; " etc. You can't assign B to B*. – thang Jan 17 '13 at 14:24
  • man, there are so many things wrong with that piece of code. I am going to start with a few. First, it's "class", not "Class". Second, the constructor for A is private! With no member to create and no friend, how the hell are you create anything of class A. Third, the constructor of B tries to use the private constructor of A. Come on dude, only your friends can touch your private. .... – thang Jan 17 '13 at 14:28
  • @thang.. sorry for the confusion. I just posted the rough draft of the code. My original code doesn't have any of the issues you mentioned. like Class, private constructor of A. – Jabez Jan 18 '13 at 00:22

3 Answers3

1
void C::getNum(A**& somenum)
{
  dObj->getNumber(static_cast<B**>(somenum)); // Error here.
}

static_cast is used to perform conversions between pointers to related classes, or to perform any other non-pointer conversion that could also be performed implicitly. which is not the case in your above example. so that's why static_cast can't be used here.

AlexDan
  • 3,203
  • 7
  • 30
  • 46
1

Because C++ supports multiple inheritance (and other features), an A * is convertible to a B * but not necessarily identical to a B *. Converting between the pointer types could move the pointer value by a some number of bytes.

As a result, it's impossible to convert an A ** to a B ** or vice versa. The base-to-derived conversion would need to happen after retrieving the value from the outer pointer.

Multiple indirection is usually a bad idea. It's hard to know what data structure might help, but a review of the standard library containers might be insightful. Anyway, thanks for condensing down this nice self-contained example!

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
1

Your compiler is doing the right thing here. In short, you can't do that. You can convert between a base class pointer and a derived class pointer, provided they point to the same object. But an array-of-Base and array-of Derived are not the same thing.

The good news is what you want to do is easier than you think. A derived class pointer already implicitly converts to a Base class pointer without casting. Assuming you know the sizes of your two arrays, and the sizes are the same, it's a simple loop:

// Given:
A** someNumbers;
B** someValues;

for (int i = 0; i < size; ++i) {
    *someNumbers[i] = *someValues[i];
}

Also, this sort of problem is why we have standard containers like vector. Some really smart people have already solved this pointer madness for you. I highly recommend taking advantage of it. It'll make your C++ experience a ton better.

Michael Kristofik
  • 34,290
  • 15
  • 75
  • 125