4 Replies - 231 Views - Last Post: 04 October 2012 - 06:24 PM Rate Topic: -----

#1 apaddobs  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 16-May 12

Object pointer based on type (reinterpret_cast)?

Posted 04 October 2012 - 08:59 AM

parent::parent ()
{
    str = "parent";
}

parent::get_str ()
{
    return str;
}

child::child ()
     : parent
{
    str = parent::str ();
    str += "'s child";
}

child::get_str ()
{
    return str;
}



In one of the modules, given a type, I want to get either parent or the child object.

returntype* get_object (const char* type)
{
if (type == 'A') then return *parent;
else if (type == 'B') then return *child;
}

How can I achieve something like above? Is it even possible? Should I have void* as the return type and then reinterpret_cast (but then again, the calling function know what object it is).
Basically, both the classes above have a post function that posts the "string" value. So at the calling level, i just want to be able to create an object called object->post. Am not sure if the calling file should have the logic to determine the type of object....Any pointers???

Is This A Good Question/Topic? 0
  • +

Replies To: Object pointer based on type (reinterpret_cast)?

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,571
  • Joined: 25-December 09

Re: Object pointer based on type (reinterpret_cast)?

Posted 04 October 2012 - 09:36 AM

My first question is why do you actually want to return a different type? It sounds more like you should be thinking of templates.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3576
  • View blog
  • Posts: 11,125
  • Joined: 05-May 12

Re: Object pointer based on type (reinterpret_cast)?

Posted 04 October 2012 - 09:48 AM

This is where polymorphism comes into the picture.

If you had something like this:
class Politician
{
public:
    virtual std::string getCampaignSlogan() = 0;
};

class Republican : public Politician
{
public:
    std::string getCampaignSlogan()
    {
        return std::string("Tax cuts!");
    }
};

class Democrat : public Politician
{
public:
    std::string getCampaignSlogan()
    {
        return std::string("No new taxes!");
    }
};

Politician * CreatePolitician(char type)
{
    switch (type)
    {
    case 'D':
    case 'd':
        return new Republican();

    case 'R':
    case 'r':
        return new Republican();
    }
    return nullptr;
}

int main()
{
    Policitian * liar = CreatePolitician(rand() % 2 ? 'D' : 'R');
    cout << liar->getCampaignSlogan();
    delete liar;
    return 0;
}



There will be a base class that exposes the common method. You, as the client, need only know that you got Politician and you can call methods on it. There is also no need to cast because both classes derive from the same base class so they can be returned freely.

This post has been edited by Skydiver: 04 October 2012 - 09:49 AM

Was This Post Helpful? 0
  • +
  • -

#4 apaddobs  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 16-May 12

Re: Object pointer based on type (reinterpret_cast)?

Posted 04 October 2012 - 05:20 PM

View PostSkydiver, on 04 October 2012 - 09:48 AM, said:

This is where polymorphism comes into the picture.

If you had something like this:
class Politician
{
public:
    virtual std::string getCampaignSlogan() = 0;
};

class Republican : public Politician
{
public:
    std::string getCampaignSlogan()
    {
        return std::string("Tax cuts!");
    }
};

class Democrat : public Politician
{
public:
    std::string getCampaignSlogan()
    {
        return std::string("No new taxes!");
    }
};

Politician * CreatePolitician(char type)
{
    switch (type)
    {
    case 'D':
    case 'd':
        return new Republican();

    case 'R':
    case 'r':
        return new Republican();
    }
    return nullptr;
}

int main()
{
    Policitian * liar = CreatePolitician(rand() % 2 ? 'D' : 'R');
    cout << liar->getCampaignSlogan();
    delete liar;
    return 0;
}



There will be a base class that exposes the common method. You, as the client, need only know that you got Politician and you can call methods on it. There is also no need to cast because both classes derive from the same base class so they can be returned freely.


Just that the politician's campaign quote is not a pure virtual function in my case. Rather, every politician will have a campaign slogan to the effect of "Remove corruption". And regardless of whether he is a democrat or a politician, there is some default slogan. And on taxes, each of them have a particular stand. Essentially, can I have the getCampaignSlogan a virtual (with body in it) instead of a pure virtual function?
Was This Post Helpful? 1
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3576
  • View blog
  • Posts: 11,125
  • Joined: 05-May 12

Re: Object pointer based on type (reinterpret_cast)?

Posted 04 October 2012 - 06:24 PM

LOL! Wouldn't we all want politicians like that. :) Yes, you can make getCampaignSlogan() a virtual function instead of a pure virtual function.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1