Thread: subclass attribute

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    35

    Question subclass attribute

    Hi!
    I've written a program in which I have a base class(named "Account") and some sub-classes. the subclasses have some extra attributes which the base class do not. there's a vector in my program, in which I keep the objects of any of the subclasses' types. since there are various subclasses and I wanna keep the built objects in a single vector, I have declared the vector to be of type "Account".
    vector<Account> accounts;
    when I wanna access the attributes exclusive to any of the subclasses via the objects of the vector "account", it gives this error:
    class Account has no member "the specified attribute X".
    I know I have declared my vector as an Account-typed one; which lacks attribute X; but the object is actually of type subclass Y and while being constructed, it has a built-in attribute Y. has it somewhat truncated Y, when being pushed_backed into the vector <Account>?
    if not, how can I access this attribute mentioned?
    Last edited by Farnaz; 03-18-2011 at 02:15 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Unfortunately, because accounts is a vector<Account>, there is no way to access those attributes as you are dealing with Account objects, not objects of the derived classes. Basically, you have the pain of type slicing.

    A few solutions exist, e.g., you could use a std::vector<std::tr1::shared_ptr<Account> > instead, or use a std::vector<Account*> and then do some manual memory management. Or if you have access to Boost, you could use a boost::ptr_vector<Account>.

    However, even if you do use any of these solutions, it would be more ideal if you made better use of polymorphism, e.g., you have a virtual member function that derived classes override such that their extra attributes are used in these overrides.

    But if you really do need to know about derived classes when dealing with this vector of (smart) pointers to Account, then use dynamic_cast to cast (with checking) from an Account pointer to the desired derived class pointer.
    Last edited by laserlight; 03-18-2011 at 02:18 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    35
    would you plz explain it a little bit more? I'm having difficulties figuring out what I should do exactly.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Farnaz View Post
    would you plz explain it a little bit more? I'm having difficulties figuring out what I should do exactly.
    What you need to do next, exactly, is look up some of the terms and jargon from laserlight's post.

    That post captures, quite simply, what you need to do and some options for doing it. However, you do need to understand the terms used to be able to apply it.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    35
    ok...guess you're right. thank you anyway.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LDAP Query
    By Travoiz in forum C++ Programming
    Replies: 0
    Last Post: 08-13-2009, 02:58 PM
  2. Any way to poll the class an attribute decorates
    By indigo0086 in forum C# Programming
    Replies: 0
    Last Post: 08-17-2008, 01:11 PM
  3. meta data and attribute
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-13-2008, 01:26 AM
  4. Friend cannot inherit from private subclass
    By MWAAAHAAA in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2006, 04:44 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM