Instantiating classes

This is a discussion on Instantiating classes within the C++ Programming forums, part of the General Programming Boards category; Right now I'm working on a program that uses a class that contains pointers to other classes. The problem I ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    8

    Instantiating classes

    Right now I'm working on a program that uses a class that contains pointers to other classes. The problem I am having is trying to figure out where to instantiate the classes using the "new" command so that they remain in memory after function that allocates/creates them. It seems to me that this should be done in the class constructor, although when I try this, the class is correctly created then deleted once the constructor exits leaving the pointer to the class to be undefined. This is how I understand the "new" command anyway. Am I correct in this statement or do I have it confused with something else, and how can I instantiate these objects so that they are not deleted and the objects that point to them left undefined at the end of the fuction?

    I have it coded as below currently:


    class ConstrLst: public xorlist
    {
    public:
    ConstrLst()
    {
    Compfunct=cmpConstrPtrs;
    }
    };


    class connector
    {
    protected:
    char id[6]; float value;
    ConstrPtr informant;
    ConstrLst *constrsP; //pointer to the above class
    public:
    connector(char *name);

    int HasValue()
    {
    return informant!=0;
    }

    void ShowName()
    {
    printf("%s", id);
    }

    float GetValue()
    {
    return value;
    }

    void SetValue(float NewValue, ConstrPtr Setter);

    void ForgetValue(ConstrPtr retractor);
    void connect(ConstrPtr NewConstraint);
    void ForEachExcept(ConstrPtr, message);
    };


    // and the constructor is:
    connector::connector(char *name)
    {
    strcpy(id, name);
    informant=0;
    constrsP=new ConstrLst;
    printf("Connector constructor\n");
    }

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    afaik, the new command allocates memory until the program closes, regardless of scope. that's why it's used.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 11:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 08:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 04:49 AM

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