compiler treatment of const etc

B

Bart Simpson

I'm quite puzzled by this. I have a class wrapped around a struct - a
ptr to which is sometimes needed by some legacy C code.

The ff sumamrizes the situation:


namespace { StructOfRawPtr nullstruct = {0} )

class WrappedPtr
{
public:
WrappedPtr:m_ptr(nullstruct){}
...
const rawPtr * RawPointer() const;

private:
rawPtr const * RawPointer();

StructOfRawPtr m_struct ;
}


//In other code

DoSomething(const rawPtr* p);

void foo(WrappedPtr& wrp)
{
//compiler complains cant access private member func for line below
DoSomething(wrp.RawPointer());
}

Why is the compiler trying to use the private function even thought the
signature of DoSomething() suggests that its the function that returns
the const rawPtr* that I need ?
 
I

Ian Collins

Bart said:
I'm quite puzzled by this. I have a class wrapped around a struct - a
ptr to which is sometimes needed by some legacy C code.

The ff sumamrizes the situation:


namespace { StructOfRawPtr nullstruct = {0} )

class WrappedPtr
{
public:
WrappedPtr:m_ptr(nullstruct){}
...
const rawPtr * RawPointer() const;

private:
rawPtr const * RawPointer();

StructOfRawPtr m_struct ;
}


//In other code

DoSomething(const rawPtr* p);

void foo(WrappedPtr& wrp)
{
//compiler complains cant access private member func for line below
DoSomething(wrp.RawPointer());
}

Why is the compiler trying to use the private function even thought the
signature of DoSomething() suggests that its the function that returns
the const rawPtr* that I need ?
You have a non-const object, so the non-const method (the private one)
will be used.
 
D

dasjotre

I'm quite puzzled by this. I have a class wrapped around a struct - a
ptr to which is sometimes needed by some legacy C code.

The ff sumamrizes the situation:

namespace { StructOfRawPtr nullstruct = {0} )

what type is StructOfRawPtr? is it at all
relevant for your question?
class WrappedPtr
{
public:
WrappedPtr:m_ptr(nullstruct){}
...

this is not syntactically correct!
what is m_ptr? shouldn't it be m_struct instead?

before you post again check
http://www.parashift.com/c++-faq-lite/how-to-post.html
const rawPtr * RawPointer() const;

private:
rawPtr const * RawPointer();

const rawPtr * and rawPtr const * are the same thing.
they are both non-const pointers to const rawPtr object.
StructOfRawPtr m_struct ;

}

//In other code

DoSomething(const rawPtr* p);

void foo(WrappedPtr& wrp)
{
//compiler complains cant access private member func for line below
DoSomething(wrp.RawPointer());

}

Why is the compiler trying to use the private function even thought the
signature of DoSomething() suggests that its the function that returns
the const rawPtr* that I need ?

return type is irrelevant here.
in foo, wrp is not const, so
rawPtr const * WrappedPtr::RawPointer();
is called, and it is private.

if you change foo to
void foo(const WrappedPtr & wrp)
DoSomething(wrp.RawPointer());
will call
const rawPtr * WrappedPtr::RawPointer() const
which is public

DS
 
R

Ron Natalie

Access specifiers (public/private) have no affect on overload
selection.

The process for invoking member functions is roughly as follows:

1. The name is looked up.
2. Overloads for that name are considered.
3. The access is checked for the matched overload
4. If the function is virtual, the final overrider is invoked
 

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
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top