1

I have the SuperClass called Transaction, it has methods:

  • Type
  • Guid
  • Customer

then the subclass called Order has these methods:

  • item
  • quantity
  • cost ...

so Order inherits from Transaction.

My problem is that there can be multiple types of transactions... Orders, Payment, etc.

Therefore i hold every type of transaction in the array like this:

Transaction trans[100];
trans[0] = Order order(val,val,val);
trans[1] = Order order(val,val,val);
trans[2] = Order order(val,val,val);
...

But now when i call trans[3].Get_item(); i get error that Transaction class has no method Get_item, yes it doesn't but what it's holding has.

I have tried making array an array of pointers and accessing using -> operator. But the problem persists.

Real Code ::::::

vector<Transaction *> trans;
....
Order order(words[0], words[1], words[2], words[3], words[4], words[5]);
trans.push_back(&order);
....
trans[i]->Getitem(); //error here.
3
  • 1
    See also: en.wikipedia.org/wiki/Object_slicing , stackoverflow.com/questions/274626/what-is-object-slicing Commented Sep 27, 2015 at 18:45
  • How do you know that trans[i] points to an Order (and not, say, a Payment), and that GetItem() call on it makes sense? If all transactions in the vector are in fact orders, then store a vector of Order*. If not all are, then it's back to "how do you plan to know which are which"? Commented Sep 27, 2015 at 18:49
  • Actually i looked again, And i Am storing type in the Transaction Commented Sep 27, 2015 at 18:51

1 Answer 1

4

An array like Transaction trans[100]; can't hold subclasses of Transaction, only Transaction itself. If you want an array to hold arbitrary Transaction child classes, make an array of pointers (raw or not doesn't matter). Example with a vector and raw pointers:

std::vector<Transaction *> t;  
t.push_bask(new Transaction()); //insert constructor params if necessary params 
t.push_bask(new Order());  
t[0]->getType();
t[1]->getType();
...
//and don't forget delete, or use smart pointers.

If you additionally need to access methods not available at all in the parent, the compiler needs to know the actual type, ie. it expects you to make an appropriate cast first. If you can't say what type it is, you can't call the method.

((Order*)t[1])->getItem(); //you can do that because you know [1] is a Order  

//but this is wrong and leads to UB:
((Order*)t[0])->getItem();  //wrong!

In some (not all) situations, dynamic_cast can help to test for some type.

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

6 Comments

can you post some example code...i did make mine to pointers, also it was vector. ` vector<Transaction *> trans;` then trans[i]->Getitem() doesn't work
i store type in Trasnaction so i should be able to use Order casting
If you can find out with getType if it's a Order then it's ok, problem solved.
Btw., your last code sample in the question has a pointer to a local variable which may be gone before the vector is. Careful with that.
@MuhammadUmer If you insert a Order in the Transaction vector (without pointers), the Order object is copied with the copy constructor of Transaction => What's in the vector is a pure Transaction, nothing else. (And with pointers, only the pointer address is copied)
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.