473,387 Members | 1,464 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,387 software developers and data experts.

constness and inheritance

struct A {
virtual int foo(const int bar);
};

int A::foo(const int bar)
{
return 0;
}
struct B : public A {
int foo(int bar);
};
int B::foo(int bar)
{
return 1;
}

int
main()
{
A *p = new B;
const int baz=-1;
int r = p->foo(baz);
return 0;
}

here B::foo is called (tried with g++3.2.3)
i wonder if it's a problem for non elementary types in parameters
since derived function can modify the baz argument with impunity
contrary to the expectations of the caller.
Granted, B::foo could've taken bar as the const parameter
and casted to non const but there are no casts of any
kind in the program above.

Jul 26 '07 #1
2 1253
sn****@gmail.com wrote:
struct A {
virtual int foo(const int bar);
};

int A::foo(const int bar)
{
return 0;
}
struct B : public A {
int foo(int bar);
This 'foo' *overrides* 'A::foo' because it has exactly same type.
};
int B::foo(int bar)
{
return 1;
}

int
main()
{
A *p = new B;
const int baz=-1;
int r = p->foo(baz);
return 0;
}

here B::foo is called (tried with g++3.2.3)
As it bloody well should.
i wonder if it's a problem for non elementary types in parameters
since derived function can modify the baz argument with impunity
contrary to the expectations of the caller.
Top-level const qualifiers are ignored in the function declaration
as far as type matching is concerned. In your program 'B::foo'
and 'A::foo' have *exactly same* type.
Granted, B::foo could've taken bar as the const parameter
and casted to non const but there are no casts of any
kind in the program above.
The top-level 'const' doesn't matter.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 26 '07 #2
On Jul 26, 8:05 pm, snd...@gmail.com wrote:
struct A {
virtual int foo(const int bar);
};
int A::foo(const int bar)
{
return 0;}
struct B : public A {
int foo(int bar);};
int B::foo(int bar)
{
return 1;
}
int
main()
{
A *p = new B;
const int baz=-1;
int r = p->foo(baz);
return 0;
}
here B::foo is called (tried with g++3.2.3)
Correct. Top level const in a function argument is ignored in
function types.
i wonder if it's a problem for non elementary types in parameters
since derived function can modify the baz argument with impunity
contrary to the expectations of the caller.
What expectations of the caller? All the caller can expect is
never to see the parameter. It's call by copy, remember.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 27 '07 #3

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

Similar topics

0
by: Dave | last post by:
Hi, I just want to share what I have found today. In the online article "Enforce Constness With C#" by Bill Wagner for "Visual Studio Magazine", he explaines how to make a little workaround to...
1
by: Richard Hayden | last post by:
Hi, I understand such pointers as 'const int* const ip' and 'const int* ip' etc., but I'm getting confused when seeing things like 'const int* const* ip' (i.e. with two or more asterisks)....
15
by: Trevor Lango | last post by:
I want to be able to cast away the constness of a private member variable in a member function of a class. I have the private data member declared as follows: const double x; I have an...
8
by: Srini | last post by:
Hello all, I was just wondering about this. A const member function guarantees constness of the object within the function body. But there's no way for a member function to guarantee the...
7
by: Olav | last post by:
Hi I wonder if it is possible to have templates expand based on constness, something like: template<typename A, typename B>copy( A &a, B &b); becoming: copy( const class AClass &a, class...
3
by: mailtogops | last post by:
Hi, I often think over the const_cast operator provided by C++ and I feel usage of const_cast or the document availale for const_cast are cumbersome. C++ provides us, const qualifier which...
14
by: PengYu.UT | last post by:
In the following program, I want an iterator contain pointer pointing to constant object not const pointer. If it is possible would you please let me know how to do it? #include...
2
by: Laurent Deniau | last post by:
I am looking for the "cleanest" way to cast away the constness of a pointee in C89, something like const_cast<T*>() in C++. Actually, I am using: #define CONST_CAST(typename,value) \ (((union {...
13
by: Javier | last post by:
Hello, I have some cuestions about constness with standard containers and pointers. Supose I have a list of pointers to some class B: std::list< B * list; I have readed that constness in...
6
by: Tarique | last post by:
Can someone please explain the idea of Logical Constness as explained in section 10.2.7.1 Of Stroustrup's "The C++ Programming Language" ,with a short working code if possible. Thank You
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.