开发者

Help with Pointers, pointers to objects, and classes

I am looking to be pointed in the right direction. I have 1 class Event

class Event{
private:
 vector<string> Question;
 char Mode;// 1 = Ascending 2 = Desce开发者_JS百科nding 3 = None
 string EventName;
public:
    Event(string Name){
        EventName = Name;
        SetQuestionSize();
        SetQuestion();
        Mode = 3; 
    }

    void SetName(string NewName){
        EventName = NewName;
    }
    void SetQuestionSize(){
        Question.resize(15);
    }

    int ReturnQuestionSize(){
        return Question.size();
    }

    void SetQuestion(){
        Question[0]="Enter ";
        Question[1]="1 ";
        Question[2]="to ";
        Question[3]="sort ";
        Question[4]="in ";
        Question[5]="ascending ";
        Question[6]="order, ";
        Question[7]="2 ";
        Question[8]="for ";
        Question[9]="Descending, ";
        Question[10]="or ";
        Question[11]="3 ";
        Question[12]="to ";
        Question[13]="ignore ";
        Question[14]=EventName;
    }

    string ReturnQuestion(int Index){
        return Question[Index];
    }

    /*vector<string> ReturnQuestion(){
 return Question;
    }*/

    void SetMode(char NewMode){
 if (NewMode == '0' || NewMode == '1' || NewMode == '2')
 Mode = NewMode;
}

    char ReturnMode(){
 return Mode;
    }

    string ReturnName(){
        return EventName;
    }
};

This is will be a member of a second object, which will use Event's functions to store data in Event's members.

The problem I'm having is declaring an array of Event objects in my second object. When researching I came across ways to use an array of pointers to the first object, and some operator '->' that I'm guessing is related to virtual functions.

class WhatTheyWant{
    Event *events[2];
public:
    WhatTheyWant(){
        events[0]= new Event("Miss");
        events[1]= new Event("Dodge");
    }
};

I'm very ignorant about pointers, and I know I will have to learn them eventually, but are they the best way to go or is there a better.


Since your Event class doesn't have a default constructor, you need to explicitly construct each object with its name, so the way you're doing it currently is the only way to do it.

If you add a default constructor to Event, you can do it in at least two other ways:

If you will always have a (small) fixed number of objects, you can just declare an array of constant size:

Event events[2];

Doing this will automatically construct the objects when WhatTheyWant is created, so you just need to set the names afterwards:

WhatTheyWant() {
  events[0].SetName("Miss");
  events[1].SetName("Dodge");
}

If you want to have a variable number of events, you can declare a single pointer and dynamically allocate an array of objects:

Event *events;

And you could probably give the number as a parameter to the constructor:

WhatTheyWant(int numEvents) {
  events = new Event[numEvents];
  for (int i = 0; i < numEvents; i++)
    events[i]->SetName("...");
}

Also, not directly related to your question, but your Mode variable would be better modeled using an enumeration instead of a char. Using an enum makes it clearer as to what the variable really means, rather than using values like 0, 1 and 2. For example:

public:
  enum ModeType { Ascending, Descending, None };
private:
  ModeType Mode;
public:
  Event() {
    ...
    Mode = Ascending;
  }
  void SetMode(ModeType NewMode) {
    Mode = NewMode;
  }
  ModeType ReturnMode() {
    return Mode;
  }


You can use either array of objects or array of pointers.

Array of objects go like below.

class WhatTheyWant{ 
    Event events[2]; 
public: 
    WhatTheyWant()
    { 
       events[0] = Event("Miss");
       events[1] = Event("Dodge");
    } 
 }; 

Note: You need to add default constructor to your event class to compile the above approach.

With the above approach, you do not need to take care of freeing Event objects. Whenever WhatTheyWant object gets destroyed, event objects get destroyed.

Array of pointers approach goes like you mentioned.

But you need to take care of freeing the memory allocated(Unless you use auto_ptr or some c++0x equivalent). Deletion should happen in destructor like below.

class WhatTheyWant{ 
    Event *events[2]; 
public: 
    WhatTheyWant(){ 
        events[0]= new Event("Miss"); 
        events[1]= new Event("Dodge"); 
    } 
    ~WhatTheyWant()
    {
        delete events[0];
        delete events[1];
    }
};


In C++, pointers are just like arrays

in your WhatTheyWant class, you define the private member:

Event *events[2];

This is an array of arrays (2D array) with variable length (of arrays) and 2 element in each array.

and the operator '->' is used when you want to access a (member of some kind of object) and that is called an object pointer (a pointer which points to an object) but when you define a normal object variable you use '.' operator.

If you've got the courage and knowledge to use them they are very useful but in general they're dangerous and that's why the new languages tend to go to the managed way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜