passing const ptr argument

A

Anthony

Hi,
I want to write protect an argument that is passed by reference.
But I still want to be able to call members of that argument.
E.g. code (bottum) will gives the following compiler error:
writeProtEvt.cpp: In member function `virtual void A::print(const B*)':
writeProtEvt.cpp:24: error: passing `const B' as `this' argument of `virtual
void B::print()' discards qualifiers

Can anyone explain how to do this the right way?

thanks in advance,
Anthony Lansbergen

// -------------------------------------------------------------------------
---
#include <ostream.h>
Class B
{
public:
virtual void print()
{
cout << "printB" << std::endl;
}
};

class A
{
public:
virtual void print(const B* b)
{
cout << "printA" << std::endl;
b->print();
}
};

main()
{
cout << "test" << std::endl;
A* a;
B* b;
a = new A();
b = new B();
a->print(b);
}
 
G

Gianni Mariani

Anthony said:
Hi,
I want to write protect an argument that is passed by reference.
But I still want to be able to call members of that argument.
E.g. code (bottum) will gives the following compiler error:
writeProtEvt.cpp: In member function `virtual void A::print(const B*)':
writeProtEvt.cpp:24: error: passing `const B' as `this' argument of `virtual
void B::print()' discards qualifiers

Can anyone explain how to do this the right way?

thanks in advance,
Anthony Lansbergen

// -------------------------------------------------------------------------
---
#include <ostream.h>
Class B
{
public:
virtual void print()
Try this :

virtual void print() const
{
cout << "printB" << std::endl;
}

Note the const at the end of the function decl. This indicates that the
"this" parameter to the function is const.
 
A

Ali Cehreli

I want to write protect an argument that is passed by reference.
But I still want to be able to call members of that argument.

This means that you can call only const member functions of those
objects.
E.g. code (bottum) will gives the following compiler error:
writeProtEvt.cpp: In member function `virtual void A::print(const B*)':
writeProtEvt.cpp:24: error: passing `const B' as `this' argument of `virtual
void B::print()' discards qualifiers

Can anyone explain how to do this the right way?

thanks in advance,
Anthony Lansbergen

// -------------------------------------------------------------------------
---
#include <ostream.h>
Class B
{
public:
virtual void print()
{
cout << "printB" << std::endl;
}
};

class A
{
public:
virtual void print(const B* b)
{
cout << "printA" << std::endl;
b->print();
}
};

Both of those member functions should be declared as const because
they don't modify the object:

virtual void print() const // <---
{
/* ... */
}

main returns 'int' and it must be specified:

int main()

Ali
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,489
Latest member
BrigidaD91

Latest Threads

Top