473,466 Members | 1,457 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

manipulating an objects through pointers or references

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
2 1388
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jan Pieter Kunst | last post by:
(apologies if this message is a duplicate -- my news server seems to have problems) Greetings, When using PHP 4, this: // ex. 1 class A { function A(&$obj) {
4
by: Vinodh Kumar | last post by:
#include <iostream> using namespace std; struct A { virtual void fa(); }; struct B : public A
9
by: Aguilar, James | last post by:
Hey guys. A new question: I want to use an STL libarary to hold a bunch of objects I create. Actually, it will hold references to the objects, but that's beside the point, for the most part. ...
6
by: Alf P. Steinbach | last post by:
One main usage of references is for arguments, to replace e.g. void foo(int*) with void foo(int&). However, our Holy Standard claims that a reference must be initialized with an _object_, before...
7
by: silversurfer2025 | last post by:
Hello, I would like to overload the operators *, + and = in a class, which is used much like a vector (such as in linear algebra). For this, I need to be able to add featureVectors to each other,...
15
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
2
by: Veloz | last post by:
Hiya My question is whether or not you should associated related objects in your software using a scheme of id's and lookups, or wether objects should actually hold actual object references to...
23
by: raylopez99 | last post by:
A quick sanity check, and I think I am correct, but just to make sure: if you have a bunch of objects that are very much like one another you can uniquely track them simply by using an ArrayList...
2
by: BobLewiston | last post by:
The concept of delegates (references to methods) per se is new to me (although I used function pointers in C years ago), and although the literature acknowledges that there are significant...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.