subclass attribute

This is a discussion on subclass attribute within the C++ Programming forums, part of the General Programming Boards category; Hi! I've written a program in which I have a base class(named "Account") and some sub-classes. the subclasses have some ...

  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
    21,883
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,420
    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%.

  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, 03:44 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21