Brainstorming: How to design this issue?

This is a discussion on Brainstorming: How to design this issue? within the C++ Programming forums, part of the General Programming Boards category; Hi, I have a class Base and many Derived classes. All the Derived are identical (the same data members and ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    11

    Brainstorming: How to design this issue?

    Hi,

    I have a class Base and many Derived classes. All the Derived are identical (the same data members and functions) just the name differs Derived1, Derived2, etc.

    Now the problem is that I receive a pointer to Derived1 and I should replay by sending a pointer to Derived3. Although all the Derived are the same I shoul still delete Derived1* and call new for Derived2 and deep copy all the contents from Derived1*.

    Any ideas how to avoid new and deep copy and still have different names for Derived classes.

    I hope I made myself understood.

    BR,
    Cristian

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >All the Derived are identical
    This should tell you something.

    >Any ideas how to avoid new and deep copy and still have different names for Derived classes.
    If they are all the same then why not just have one derived class with a data member that is used to differentiate them from each other? You may have good reasons for having multiple classes that only differ in name, but usually this is the sign of a design flaw.

    -Prelude
    Last edited by Prelude; 08-21-2002 at 07:07 PM.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Cristian,

    I have a type Base and two identical types DerivedOne and DerivedTwo that are derived from Base.

    I receive a Base * I know is a DerivedOne*.
    Could I cast from DerivedOne* to DerivedTwo?
    If yes how?

    BR,
    Cristian

    PS
    processMessage(Base * msg)
    {
    DerivedOne* pDerivedOne = dynamic_cast<DerivedOne *> msg;
    HOW TO DO
    DerivedTwo * pDeriveTwo = CAST<> pDerivedOne? Is it possible?
    }
    Technically not the same question. I'm being overly-sensitive to cross-posting, right?

    Thought so...

    If you had a virtual function in the "base" class, I might understand the need for multiple, derived classes of the same type. As it is, I'm at a loss to answer your question. Sorry.

    (Offhand, I'd say that you're making this more difficult than it needs to be, i.e. as though you feel that you need a derived class for each object instantiated.)

    Follow Prelude's advice. (My "pat" answer when I don't have a clue. )

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    11
    Hi,

    Thanks Prelude and skipper.
    I agree with you and I made the same proposition to my colleagues before: to have one class with a extra member "name": the reason the design was that they want to have the name of the class in the error logs... ;0).

    BR,
    Cristian.

    PS: It was the same question...

  5. #5
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    You can use condtional compilation. IE,

    #ifndef NDEBUG
    log << "Whatever::methodName()"
    #endif

    and then define NDEBUG if you wish to get rid of the logs.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  6. #6
    Registered User
    Join Date
    Aug 2002
    Posts
    11
    OK, I may be saying a stupid thing as I didn't worked with multiple inheritance but here it is:
    Base
    / | \
    Derived1 - Derived2 Derived3

    This should be read as:
    Derived1, Derived2, Derived2 are Base
    Derived2 is Derived1

    So now when I receive a message I could do the following:
    processMessage(Base * message) {
    if (message is Derived1)
    {
    I process my message and send it back as Base*;
    }

    When the message is received the receiver would be able to make the cast to Derived2 because Derived2 is also a Derived 1. Am I right?, I get enthusiastic now...

    If you wonder how I now which class a message is the answer is: by a field in the Base class that will be filled at the construction time.

    I'm wondering if I am right?. I'll try to check myself too.

    BR,
    Cristian


    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Software Design issue
    By zacs7 in forum Tech Board
    Replies: 1
    Last Post: 04-22-2009, 05:34 AM
  2. Design Issue, Your Thoughts Wanted
    By IdioticCreation in forum C++ Programming
    Replies: 5
    Last Post: 02-02-2008, 06:28 PM
  3. Replies: 3
    Last Post: 11-16-2006, 03:23 AM
  4. dll : design issue!
    By dug in forum C++ Programming
    Replies: 8
    Last Post: 02-17-2005, 10:48 AM
  5. Design Issue.
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2002, 11:27 PM

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