LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-26-2010, 09:56 AM   #1
Lobinho
Member
 
Registered: May 2010
Distribution: Ubuntu
Posts: 72

Rep: Reputation: 18
Thumbs up Dynamic list with different type - C++


Hi,

I'm quite new to c++ programming, so my concepts are not so increased yet...

I wanna to create a list to store my objects. I'll resume them here:
Code:
class Event { //I tried to built this class like an "interface" on java
public:
  Event(int timeout); //on .cpp I'll do: this->timeout = timeout
  virtual ~Event();
 
private:
  int timeOut; //event timeout - all Events will have one

public:
  int getTimeout();

  //The message is particular to each Event
  virtual int getMessage(char* message) = 0; //pure virtual method
}

class EventA : public Event {
public:
  EventA(int timeout);
  ~EventA();

private:
  int attributeOne; 
  int attributeTwo;

public:
  int getMessage(char* message); 
  //this is implemented on cpp:
  /* int EventA::getMessage(char* message) { 
   *   strcpy(message, "EventA msg");
   * }
   */
}


class EventB : public Event {
public:
  EventB(int timeout);
  ~EventB();

private:
  int attributeOne;
  bool attributeTwo; 
  char attributeThree[128];

public:
  int getMessage(char* message); 
  //this is implemented on cpp:
  /* int EventB::getMessage(char* message) { 
   *   strcpy(message, "EventB msg");
   * }
   */
}

...

And what I want to do is an event list:
Code:
Event** eventList = new Event*[5];
eventList[0] = new EventA(4);
eventList[1] = new EventB(6);
eventList[2] = new EventC(8);
eventList[3] = new EventD(1);
eventList[4] = new EventE(2);

char temp[128] = "";
eventList[0]->getMessage(temp);
printf("%s\n", temp);
eventList[1]->getMessage(temp);
printf("%s\n", temp);
eventList[2]->getMessage(temp);
printf("%s\n", temp);
...
Sometimes this works and sometimes I got "segmentation fault".

I think I'm doing it wrong, cause these objects have different sizes, so I don't know if I could have all of them in a single array.

Someone have any tip or tutorial or how to to share with me?



Thanks in advance!

Last edited by Lobinho; 06-26-2010 at 11:03 AM. Reason: updating sample code with "Event" instead of "Events"
 
Old 06-26-2010, 11:01 AM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,223

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Replace strcpy with something that can't segfault the program. The best practice would be to use std::string and its = operator.

Also, the class is Events in the first code sample and Event in the second code sample

Last edited by dugan; 06-26-2010 at 11:02 AM.
 
Old 06-26-2010, 11:27 AM   #3
Lobinho
Member
 
Registered: May 2010
Distribution: Ubuntu
Posts: 72

Original Poster
Rep: Reputation: 18
Thanks for the tip dugan.

I never used the std::string before (I don't know why...), now I'll implement the modifications on my system to use std::string.
But the doubt about an array list with different objects inside still remains.
 
Old 06-26-2010, 11:49 AM   #4
Lobinho
Member
 
Registered: May 2010
Distribution: Ubuntu
Posts: 72

Original Poster
Rep: Reputation: 18
When I do:

I have the method:

Code:
void setMessage(std::string message) {
  this->message = message;
}
When I do this... I'm copying the content of message to this->message or copying the pointer of message to this->message?
 
Old 06-26-2010, 02:15 PM   #5
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,223

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Quote:
Originally Posted by Lobinho View Post
Code:
this->message = message;
I'm copying the content of message to this->message or copying the pointer of message to this->message?
The former.

Last edited by dugan; 06-26-2010 at 02:21 PM.
 
Old 06-26-2010, 03:11 PM   #6
Lobinho
Member
 
Registered: May 2010
Distribution: Ubuntu
Posts: 72

Original Poster
Rep: Reputation: 18
Thanks dugan... I changed my code and now is better and more readable.
The array for objects is working fine.
 
Old 06-26-2010, 03:32 PM   #7
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,223

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Glad to hear it. BTW, this would be more in line with current C++ best practices:

Code:
#include <vector>
#include <string>
#include <iostream>

using namespace std;

class event
{
public:
    virtual string get_message() = 0;
};

class event_a: public event
{
public:
    string get_message();
};

class event_b: public event
{
public:
    string get_message();
};

string event_a::get_message()
{
    return "Event A Message";
}

string event_b::get_message()
{
    return "Event B Message";
}

int main()
{
    vector<event *> event_list;
    event_list.push_back(new event_a);
    event_list.push_back(new event_b);

    event *a = new event_a;
    a->get_message();
  
    for (vector<event *>::iterator itr = event_list.begin(); itr != event_list.end(); ++itr)
    {
        cout << (*itr)->get_message() << endl;
        delete *itr;
    }
    return 0;
}
Or if you want to use smart pointers:

Code:
#include <vector>
#include <string>
#include <iostream>
#include <boost/shared_ptr.hpp>

using namespace std;
using namespace boost;

class event
{
public:
    virtual string get_message() = 0;
};

class event_a: public event
{
public:
    string get_message();
};

class event_b: public event
{
public:
    string get_message();
};

string event_a::get_message()
{
    return "Event A Message";
}

string event_b::get_message()
{
    return "Event B Message";
}

int main()
{
    typedef shared_ptr<event> event_ptr;
    vector<event_ptr> event_list;
    event_list.push_back(event_ptr(new event_a));
    event_list.push_back(event_ptr(new event_b));

    for (vector<event_ptr>::iterator itr = event_list.begin(); itr != event_list.end(); ++itr)
    {
        cout << (*itr)->get_message() << endl;
    }
     
    return 0;
}

Last edited by dugan; 06-26-2010 at 03:57 PM.
 
1 members found this post helpful.
Old 06-26-2010, 04:16 PM   #8
Lobinho
Member
 
Registered: May 2010
Distribution: Ubuntu
Posts: 72

Original Poster
Rep: Reputation: 18
I don't know vectors, but they look like arraylist on java...
I'll study them too!

I'm build some applications for an embedded system, then I was trying to use light resources. Do you know if these classes (vectors, strings...) will make too much difference (running on 400mhz processor and 64mb ram)?
I'll try to find out how much memory they are occupying with sizeof() and then try to build some examples with vectors.

Thanks again dugan!
 
Old 06-26-2010, 04:35 PM   #9
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,223

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
They don't have enough overhead to make any difference at all.

Last edited by dugan; 06-26-2010 at 04:37 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] List all files by type loftus49 Linux - Newbie 4 06-22-2010 01:20 PM
Dynamic array of type 'Handle' in WaitforMultipleObject in Windows Kunsheng Programming 5 10-29-2009 10:46 AM
apt-get; E: Type 'GPG' is not known on line 20 in source list /etc/apt/sources.list tomorrow Linux - Newbie 9 05-21-2009 08:42 AM
list with dynamic array TurtleFace Programming 7 11-05-2006 06:58 PM
list<type> how can I make type be a pointer? exodist Programming 2 06-06-2005 08:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration