By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,344 Members | 1,853 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,344 IT Pros & Developers. It's quick & easy.

manipulating an objects through pointers or references

P: n/a
Hi

Stroustrup p.312 second paragraph after the code.
" To get polymorphic behavior in C++, the member function called must
be virtual and objects must be manipulated through pointers or
references."

the code below fulfills the virtual part, can someone please show me
how the second part is done? i.e, manipulating the object through
pointers or references.

thank you
//task.h
************************************************** **************
#ifndef TASK_H
#define TASK_H
#include <string>

class Task {
public:
virtual void print_mi()=0; // print menu item
};

class Thr_task : public Task {
std::string m_item;
public:
Thr_task(const std::string&);
void print_mi();
};

#endif
//task.cpp
************************************************** **************
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;

using namespace std;

#include "task.h"

Thr_task::Thr_task(const string& n) :
m_item(n)
{
}

void Thr_task::print_mi() {
cout << m_item << endl;
}
//main.cpp
************************************************** **************
#include "task.h"
using std::vector;
using namespace std;

int main() {
Thr_task t1("preform Thread task.");

vector<Task*vTp;
vTp.push_back(&t1);

vTp[0]->print_mi();
Nov 10 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Gary Wessle wrote:
Stroustrup p.312 second paragraph after the code.
" To get polymorphic behavior in C++, the member function called must
be virtual and objects must be manipulated through pointers or
references."

the code below fulfills the virtual part, can someone please show me
how the second part is done? i.e, manipulating the object through
pointers or references.
[..]

vector<Task*vTp;
vTp.push_back(&t1);

vTp[0]->print_mi();
Yes, since you had to use ->, it's "through a pointer".

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 10 '06 #2

P: n/a

Gary Wessle wrote:
Hi

Stroustrup p.312 second paragraph after the code.
" To get polymorphic behavior in C++, the member function called must
be virtual and objects must be manipulated through pointers or
references."

the code below fulfills the virtual part, can someone please show me
how the second part is done? i.e, manipulating the object through
pointers or references.

thank you
//task.h
************************************************** **************
#ifndef TASK_H
#define TASK_H
#include <string>

class Task {
public:
virtual void print_mi()=0; // print menu item
};

class Thr_task : public Task {
std::string m_item;
public:
Thr_task(const std::string&);
void print_mi();
};

#endif
//task.cpp
************************************************** **************
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;

using namespace std;

#include "task.h"

Thr_task::Thr_task(const string& n) :
m_item(n)
{
}

void Thr_task::print_mi() {
cout << m_item << endl;
}
//main.cpp
************************************************** **************
#include "task.h"
using std::vector;
using namespace std;

int main() {
Thr_task t1("preform Thread task.");

vector<Task*vTp;
In the above container, you are storing pointers to *any* type derived
from Task.
The compiler will complain if print_mi() is not available for type Task
and derivatives.
vTp.push_back(&t1);
loads the element's address into a new slot, C++ compiler does that
with a type check.
>
vTp[0]->print_mi();
Here, the element at vTp[0] is a pointer to a Task derivative.
Therefore, the call to print_mi() is -polymorphic.
Thats probably implemented using a virtual table on your platform, but
not necessarily.
p_element->vtable_lookup->behaviour()

If you need a decent description of that process, download Eckell's
Thinking in C++ Vol1 at mindview and read the first few chapters.
http://mindview.net/Books/TICPP/ThinkingInCPP2e.html

Nov 10 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.