• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Strange inheritance?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Strange inheritance?


  • Subject: Strange inheritance?
  • From: "Richard L. Aurbach" <email@hidden>
  • Date: Mon, 15 Mar 2010 15:34:20 -0500

I am running up against something strange. It's probably me. But it sure isn't working as I would have expected.

I am writing straight C++ in Xcode 3.1.2.

Consider the following:

class   Base    {
public:
                    Base (argT * arg1);
    virtual         ~Base () {}

    virtual void    Build () {}
    virtual bool    Func1 () {return true;}
};


class Sub1 : public Base { public: Sub (argT * arg1, argT2 * arg2); virtual ~Sub () {}

    virtual void    Build ();
    virtual bool    Func1 ();
};


class Caller { public: Caller () : mSubs() {}

    virtual         ~Caller () {}

            void    Setup ();

            void    Action (UInt16 index);
private:
    vector<Base*>   mSubs;
};

void
Caller::Setup ()
{
    ...
    Base *          base = new Sub1(arg1, arg2);
    mSubs.push_back(base);
    base->Build();              // [1] Sub::Build is called

    ...
}


void Caller::Action (UInt16 index) { Base * base = mSubs[index]; if (base->Func1()) { // [2] Base::Func1 is called !! // do something } }

Here, Base is a standard interface base-class to a set of specializations (Sub1, Sub2, etc). The Caller module works with these specializations by calling Setup (a factory method) which initializes each instance and stores its pointer in a vector. Then the Caller's Action method looks up the desired specialization and executes functions.

Specifically, in Caller::Setup, the line marked with [1] works as expected -- it calls Sub::Build();

BUT, in Caller::Action, the line marked with [2] calls Base::Func1(), rather than my expected result of Sub::Func1().

It seems to me that either: (1) I'm being particularly stupid here and would greatly appreciate straightening out; or (2) this is legitimately behaving in an other-than-expected manner.

Am I being crazy??


Cheers,

Rick Aurbach, Ph.D.
President and Chief Engineer
Aurbach & Associates, Inc.
8233 Tulane Avenue, Suite B
St. Louis, MO 63132

www:     http://www.aurbach.com/
eMail:    email@hidden [business]
               email@hidden [personal]
Fax:        314/678-0869
Phone:   800/774-7239
                314/726-1321

"Cowardice asks the question, 'Is it safe?' Expediency asks the question, 'Is it politic?' Vanity asks the question, 'Is it popular?' But, conscience asks the question, 'Is it right?' And there comes a time when one must take a position that is neither safe, nor politic, nor popular but one must take it because one's conscience tells one that it is right." -Martin Luther King, Jr. (1929-1968)

"The saddest aspect of life right now is that science gathers knowledge faster than society gathers wisdom." -Isaac Asimov (1920-1992)

"If in the last few years you haven't discarded a major opinion or acquired a new one, check your pulse. You may be dead." - Gelett Burgess (1866-1951)

"A life spent making mistakes is not only more honorable, but more useful than a life spent doing nothing." - George Bernard Shaw (1856-1950)

"You can judge your age by the amount of pain you feel when you come in contact with a new idea." - John Nuveen (1896-1968)

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rich Cook (1944-)








_______________________________________________ Do not post admin requests to the list. They will be ignored. Xcode-users mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
  • Follow-Ups:
    • RE: Strange inheritance?
      • From: Jeff Laing <email@hidden>
    • Re: Strange inheritance?
      • From: Jens Alfke <email@hidden>
  • Prev by Date: Re: Auto Indent
  • Next by Date: Re: Strange inheritance?
  • Previous by thread: Analyze project to find missing or unreferenced files
  • Next by thread: Re: Strange inheritance?
  • Index(es):
    • Date
    • Thread