deleting dynamic object problem

This is a discussion on deleting dynamic object problem within the C++ Programming forums, part of the General Programming Boards category; I have 3 different classes inherited from 1 base class. Objects are instantiated dynamically. My base class is called Sensor ...

  1. #1
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164

    deleting dynamic object problem

    I have 3 different classes inherited from 1 base class.
    Objects are instantiated dynamically.

    My base class is called Sensor and my 3 inherited classes are called typeA, typeB and typeC

    The user can pick which object they want to instntiate
    Code:
       if (type == 1) {
          head();
          sensor_a[Sensor::GetSensorCount() + 1] = new typeA;
       } else if (type == 2) {
          head();
          sensor_b[Sensor::GetSensorCount() + 1] = new typeB;
       } else {
          head();
          sensor_c[Sensor::GetSensorCount() + 1] = new typeC;
       }
    a running total of objects derrived from the 3 classes is kept in the base class and the objects are given an identity number from this.

    If I want to remove an object how can I do this as I only know the object identity number?

    e.g.
    Code:
       cout << "Enter sensor number to remove from the network: ";
       cin >> num;
       delete sensor[num];
       sensor[num] = NULL;
    obviously this won't work as my objects are not called sensor.


    I have a function in my base class
    Code:
    type Sensor::GetSenType() {
       return sensor_type;
    }
    which refers to an enumarated type
    Code:
    enum type {type_a = 1, type_b, type_c};
    and if I could get to this it would return what type (a, b or c) that particular object was and I could give the delete command the correct type.


    Is there a way to do this?

    Any comments appricieted.

    Thanks
    Open source isn't a matter of life or death......
    .......its much more important than that!!


    SuSE Linux - GCC 3.4.2
    XP Pro - Visual Studio 2005 TS, MinGW 3.4.2

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Maybe you ought to make a single sensor array with three elements instead of the three variables sensor_a, sensor_b and sensor_c.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164
    I can't. This assignment is for an OO class, thus the whole reason for the assignment is to implement inheritence, aggregation and association.
    Open source isn't a matter of life or death......
    .......its much more important than that!!


    SuSE Linux - GCC 3.4.2
    XP Pro - Visual Studio 2005 TS, MinGW 3.4.2

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,662
    Have you studied polymorphism yet?
    Are you sure you're supposed to have 3 arrays, and not one array of type Sensor?

    gg

  5. #5
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164
    No, polymorphism is the next topic. If you think this will help solve my problem I'll jump ahead and do some reading tonight.

    Deffinitely meant to be 3 different classes.
    Each derrived class has all the attributes of the base class but they each have additional functionality added.

    e.g. sensor A measures everything in the base class plus oxygen content, sensor B has the base class plus sea temperature, etc...
    Last edited by eth0; 05-17-2004 at 09:29 AM. Reason: spelling
    Open source isn't a matter of life or death......
    .......its much more important than that!!


    SuSE Linux - GCC 3.4.2
    XP Pro - Visual Studio 2005 TS, MinGW 3.4.2

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I don't think anything prevents you from having only one array. Certainly not what you told us about the assignment.
    Code:
    Sensor *sensors[MAX_SENSORS];
    sensors[0] = new typeA;
    sensors[1] = new typeB;
    sensors[2] = new typeC;
    That's polymorphism btw, so read up on it. Make sure your destructors are marked as virtual.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164
    Can I just ask someone to go a little deeper into what CornedBee has said.
    It sounds like the answer I'm looking for, however polymorphism is a large topic and I haven't come accross my method yet.

    Thanks.
    Open source isn't a matter of life or death......
    .......its much more important than that!!


    SuSE Linux - GCC 3.4.2
    XP Pro - Visual Studio 2005 TS, MinGW 3.4.2

  8. #8
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    typeA, typeB and typeC are all inherited from Sensor, therefore, a Sensor * pointer can also point to objects of typeA, typeB and typeC. When you delete everything you can just do:
    Code:
    delete [] sensors[0];
    delete [] sensors[1];
    delete [] sensors[2];
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Errm, that would be without the [] for the deletes. The array elements are single objects.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164
    This was the way I did it originally, however I came accross a problem when trying to refference data stored in the derrived objects so I assumed you couldn't do it that way.

    Here is my code using the method you suggested:

    Code:
       type = sensors[num]->GetSenType();
       
       switch (type) {
          case 1 : cout << "Sensor type: A\n"; break;
          case 2 : cout << "Sensor type: B\n"; break;
          case 3 : cout << "Sensor type: C\n"; break;
       }
       cout << "Air temperature: " << sensors[num]->GetAirTemp() << endl;
       cout << "Wind speed: " << sensors[num]->GetWindSpeed() << endl;
       cout << "Wind direction: " << sensors[num]->GetWindDir() << endl;
       if (type == 2)
          cout << "Sunlight intensity: " << sensors[num]->GetSunInt() << endl; //<---problem line
       if (type == 3) {
          cout << "Sea temperature: " << sensors[num]->GetSeaTemp() << endl;  //<---problem line
          cout << "Oxygen content: " << sensors[num]->GetOxCont()) << endl;   //<---problem line
       }
       cout << "Longitude " << sensors[num]->GetLong() << endl;
       cout << "Latitude " << sensors[num]->GetLat() << endl;
    When I try to refference the data in the the derrived objects I get an error ' no matching function for call to `Sensor::GetSunInt()' '

    How can I access this data?

    Thanks
    Open source isn't a matter of life or death......
    .......its much more important than that!!


    SuSE Linux - GCC 3.4.2
    XP Pro - Visual Studio 2005 TS, MinGW 3.4.2

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You have to cast it.
    Code:
    switch (type) {
      case 1: {
        // This doesn't seem to have any special handling
      } break;
    
      case 2: {
        SunSensor ........ = dynamic_cast<SunSensor *>(sensors[num]);
        cout << ss->GetSunInt();
      } break;
    
      case 3: {
        // Same here.
      } break;
    }
    You may have to enable RTTI in the compiler options to make dynamic_cast work.

    However, it would probably make more sense to have one virtual function PrintInformation in the Sensor class which each derived class overrides to write their own data, because that's what polymorphism is about.

    If you're not using Internet Explorer, take a look at what I've got to say on the topic.
    http://stud3.tuwien.ac.at/~e0226430/...l#polymorphism
    http://stud3.tuwien.ac.at/~e0226430/...eclasses.xhtml

    If you are using IE, it will try to download the files instead of recognizing them for what they are, so get a better browser.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    What would you consider to be a better browser?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  13. #13
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164
    Mozilla.
    You'll never use IE again.

    I use it on both Windows and Linux systems.
    Open source isn't a matter of life or death......
    .......its much more important than that!!


    SuSE Linux - GCC 3.4.2
    XP Pro - Visual Studio 2005 TS, MinGW 3.4.2

  14. #14
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Just downloaded Mozilla, very nice! Thanks for the tip.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  15. #15
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164
    Quote Originally Posted by CornedBee
    However, it would probably make more sense to have one virtual function PrintInformation in the Sensor class which each derived class overrides to write their own data, because that's what polymorphism is about.
    OK, that bit makes sense to me. I have now put a function in my classes
    Code:
       virtual void PrintInformation();
    and now in the derrived class implementation I will have the following

    Code:
    void PrintInformation() {
       cout << "Air temperature: " << sensors[num]->GetAirTemp() << endl;
       cout << "Wind speed: " << sensors[num]->GetWindSpeed() << endl;
       cout << "Wind direction: " << sensors[num]->GetWindDir() << endl;
       cout << "Sea temperature: " << sensors[num]->GetSunInt() << endl; 
       cout << "Longitude " << sensors[num]->GetLong() << endl;
       cout << "Latitude " << sensors[num]->GetLat() << endl;
    }
    obviously sensors[num] is not known here, so how do you get the function to display what you want?

    Thanks
    Last edited by eth0; 05-19-2004 at 09:19 AM.
    Open source isn't a matter of life or death......
    .......its much more important than that!!


    SuSE Linux - GCC 3.4.2
    XP Pro - Visual Studio 2005 TS, MinGW 3.4.2

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-07-2009, 05:51 PM
  2. Problem accessing calling object members- please help
    By Tiansen in forum C++ Programming
    Replies: 7
    Last Post: 11-11-2008, 06:07 AM
  3. Template object creation problem
    By babu198649 in forum C++ Programming
    Replies: 7
    Last Post: 09-16-2008, 04:02 AM
  4. Dynamic Array Problem
    By adc85 in forum C++ Programming
    Replies: 2
    Last Post: 03-04-2003, 01:29 PM
  5. dynamic object initialisation??
    By splendid bob in forum C++ Programming
    Replies: 2
    Last Post: 07-04-2002, 12:35 PM

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