473,385 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

passing const ptr argument

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);
}

Jul 22 '05 #1
2 2706
Anthony wrote:
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.
{
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);
}

Jul 22 '05 #2
On Wed, 11 Aug 2004 10:06:54 +0200, Anthony wrote:
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()


main returns 'int' and it must be specified:

int main()

Ali

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Alex Vinokur | last post by:
Various forms of argument passing ================================= C/C++ Performance Tests ======================= Using C/C++ Program Perfometer...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
25
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default...
6
by: bob_jenkins | last post by:
{ const void *p; (void)memset((void *)p, ' ', (size_t)10); } Should this call to memset() be legal? Memset is of type void *memset(void *, unsigned char, size_t) Also, (void *) is the...
17
by: Charles Sullivan | last post by:
The library function 'qsort' is declared thus: void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); If in my code I write: int cmp_fcn(...); int...
0
by: wellingj | last post by:
A little back ground on what I'm trying to do: I'm making a generic weighted graph class (vertexes and edges althought I don't call them that) to implement some pathfinding algorithms like A* and D*....
8
by: Ruben | last post by:
error: passing `const Weight' as `this' argument of `float Weight::wgt()' discards qualifiers seems to be some sort of standard error format that I'm not understanding. I have code that...
4
by: puzzlecracker | last post by:
How can I pass a reference to a method as constant? I tried the following: Function(const Foo f) or Function(readonly Foo f) Also, How to declare local variable to be constant const...
18
by: sanjay | last post by:
Hi, I have a doubt about passing values to a function accepting string. ====================================== #include <iostream> using namespace std; int main() {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.