CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2001
    Posts
    73

    dynamic object creation

    Hi Gurus!

    I need your help on this one...

    I got a wizard...the user is selecting wich program he want to convert his address book from...I've made several object that got the same function to be call...

    so, depend on the selection the user has made...I want to create my addrbook object and after use it..because the function are all the same for each objet...I don,t want to write 8 time the same code....so i've proceed with a switch as you can see with this sample of code...

    But....my addrbook object is no longer valide when I get out of the switch and want to use it..what can I do ???

    thanks a lot!!

    ViNcE...

    P.S. sorry for my bad english...speaking french





    switch(iChoix)
    {
    case 0:
    CNSv2v3 addrBook;
    break;
    case 1:
    CNSv4 addrBook;
    break;
    case 2:
    CNSv6 addrBook;
    break;
    case 3:
    COutExp addrBook;
    break;
    case 4:
    CLDIF addrBook;
    break;
    case 5:
    CCSV addrBook;
    break;
    case 6:
    COut979820002002 addrBook;
    break;
    case 7:
    CTabText addrBook;
    break;
    case 8:
    CMSExchange addrBook;
    break;
    }


    //Création du contact.
    CONTACT contact;

    addrBook.GetContact(1,&contact);






  2. #2
    Join Date
    Dec 1999
    Location
    Israel
    Posts
    2,851

    Re: dynamic object creation

    The reason this happens is because the object is created on the stack. This is ok, until you go out of scope: A variable declared in a function is destroyed when the function is over, a variable declared inside an if is destroyed at the end of the if block, and a variable declared inside a switch case block is also destroyed when the switch block is closed. You can solve this be using a pointer to an object(declared outside the switch, and a call to new to allocate the object inside the switch).

    Got a question? try looking it up in MSDN first. Msdn comes with the Visual Studio, and can be found at http://msdn.microsoft.com
    ---===---
    I'm not here for the rates, but rating a post is a good way for me to know how much i helped.
    ---===---
    Daniel

  3. #3

    Re: dynamic object creation

    with the assumption that all of your classes derive from a common class you can do the following:


    CMyAddressBookInterface *pInterface;

    switch(iChoix)
    {
    case 0:
    pInterface = new CNSv2v3;
    break;
    case 1:
    pInterface = new CNSv4;
    break;
    //other cases follow
    }

    CONTACT aContact;
    pInterface->getContact(1, &aContact);
    delete pInterface;




    Of course all the other considerations must apply about scoping rules etc.

    --------------------------------------------------------------------------------------
    Its really a simple problem, once you've worked it out.

  4. #4
    Join Date
    Jan 2001
    Posts
    73

    Re: dynamic object creation

    I knew about the stack thing...I just don't know how to solve my problem.

    ok yeah....this is what I was thinking....but

    because I got different type of objet...

    so....what is my pointer declaration ?

    I can't declar my pointer like this :

    CNSv4 * addrBook = NULL;



    and after in my switch using it like this no ?

    addrBook = new COutExp;



    can I do this???

    thanks!


  5. #5
    Join Date
    Jan 2001
    Posts
    73

    Re: dynamic object creation

    thanks...ok hmmm

    If I understand things right....
    CMyAddressBookInterface is the base class..

    It contain virtual functions....

    and I only wrote code into my derived class..CNSv4...CNSv2v3 and etc....

    is it that ?

    thanks!


  6. #6
    Join Date
    Dec 1999
    Location
    Israel
    Posts
    2,851

    Re: dynamic object creation

    In reply to:


    If I understand things right....




    it doesn't get righter than that!

    Got a question? try looking it up in MSDN first. Msdn comes with the Visual Studio, and can be found at http://msdn.microsoft.com
    ---===---
    I'm not here for the rates, but rating a post is a good way for me to know how much i helped.
    ---===---
    Daniel

  7. #7

    Re: dynamic object creation

    Yeah, something like this:


    class CONTACT; //forward declare the contact type
    class CMyAddressBookInterface
    {
    public:
    virtual void getContact(int nIndex, CONTACT *pContact) = 0;
    };


    class CONTACT; //forward declare the contact type
    class CNSv4 : public CMyAddressBookInterface
    {
    virtual void getContact(int nIndex, CONTACT *pContact)
    {
    //this example has this method inline, but the method doesn't HAVE to be inlined
    }
    };




    --------------------------------------------------------------------------------------
    Its really a simple problem, once you've worked it out.

  8. #8
    Join Date
    Jan 2001
    Posts
    73

    Re: dynamic object creation

    last question ;-)

    can my private member like int iNbContact;

    be declare into the base class.. ?

    If yes....do I have to put virtual before the declaration ?

    thanks...

    my knowledge is not very big around virtual things

    ViNcE


  9. #9

    Re: dynamic object creation

    It can be, but I prefer to use interfaces as just interfaces...therefore they have no data members. One of the things that you'll have to consider if you do add the data variable as a private member of your base interface class is that you'll want a constructor to initialize the variable. Its all a matter of your style, and it can certainly be done.

    --------------------------------------------------------------------------------------
    Its really a simple problem, once you've worked it out.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured