Hi,
I created a vector like that" typedef std::vector<Cli ent*> Clients" and declared a vector "Clients clients". I specialized the Client class to SpecialClient, so I have:
I declared the vector as
So I can store Client, SpecialClient and any other derived type of Client that I may need in the future.
My function populates the "clients" vector and return it to a calling program. So I have:
The function returns:
In the main function I try to cast the vector elements to SpecialClient but it's not working correct.
What happens is that the "name" is printed correctly but the "details" not. The cast occurs fine but the contect of the details member is not printed. I guess I'm missing some memory allocation but I can't figure out the problem. I'll appreciatte if you can help me.
Thank you.
Nei
I created a vector like that" typedef std::vector<Cli ent*> Clients" and declared a vector "Clients clients". I specialized the Client class to SpecialClient, so I have:
Code:
class Client
{
public:
/// Constructor
Client (void);
/// Destructor
virtual ~Client (void);
/// Register the client
virtual void show_info (char* name) ;
/// Get the name.
void set_name (const char * name);
private:
std:string name;
}
class SpecialClient
{
public:
/// Constructor
SpecialClient (void);
/// Destructor
virtual ~SpecialClient (void);
/// Register the client
virtual void show_info (char* name) ;
/// Get the details.
void set_details (const* char details);
private:
std:string details;
}
Code:
typedef std::vector<Client*> Clients; Clients clients;
My function populates the "clients" vector and return it to a calling program. So I have:
Code:
SpecialClient* cl = new SpecialClient();
cl->set_name("Client1");
cl->set_details("Details client 1");
clients.push_back(cl);
Code:
Clients obtain_clients () {
return (clients);
}
Code:
Clients cls = obtain_clients();
int index;
for(index=0; index < cls.size(); index++) {
Client* cl = (cls.at(index));
std::string name = cl->get_name();
std::cout << "Name: " << name << std::endl;
SpecialClient * scl = dynamic_cast<SpecialClient*>(cl);
std::string details = scl->get_details();
std::cout << "Details: " << details << std::endl;
}
Thank you.
Nei
Comment