Code still compile when there is a mis-match in .h and .cpp

Y

yinglcs

Hi,

In my .h file, I declare this:
class A{
public:
void f1 (const B* bPtr);
};

But In my .cpp, I do this:

void A::f1(B* bPtr) {
//....
}

My question is I miss 'const' in my implementation (.cpp), why the
code still compile?
 
A

Alf P. Steinbach

* (e-mail address removed):
Hi,

In my .h file, I declare this:
class A{
public:
void f1 (const B* bPtr);
};

But In my .cpp, I do this:

void A::f1(B* bPtr) {
//....
}

My question is I miss 'const' in my implementation (.cpp), why the
code still compile?

If you had the 'const' in a slightly different place, the compiler would
have been right.

As it is, perhaps there are options you can turn on to make it more
standard-conforming.

Or, use some other compiler.
 
G

Gavin Deane

Hi,

In my .h file, I declare this:
class A{
public:
void f1 (const B* bPtr);

};

But In my .cpp, I do this:

void A::f1(B* bPtr) {
//....

}

My question is I miss 'const' in my implementation (.cpp), why the
code still compile?

It shouldn't compile, for exactly the reason you suggest - unless, of
course, your class A *also* contains an overload of f1 declared as
void f1 (B* bPtr);
in the class.

If not, maybe something else in your code is causing your confusion.
You've posted a snippet that I can't directly compile so I don't know
what else is in your code. See posting guidelines at
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8

Try this complete program. Does it compile for you? What compiler are
you using?

class B {};

class A{
public:
void f1 (const B* bPtr);
};
void A::f1(B* bPtr) {}

int main() {}

Gavin Deane
 
Y

yinglcs

* (e-mail address removed):








If you had the 'const' in a slightly different place, the compiler would
have been right.

As it is, perhaps there are options you can turn on to make it more
standard-conforming.

Or, use some other compiler.
Thanks. Can you please tell me what is the place where the compiler
would have been right?
 
A

Alf P. Steinbach

* (e-mail address removed):
Thanks. Can you please tell me what is the place where the compiler
would have been right?

Why? I have the feeling that the original question was contrived. That
you simply /got the question wrong/.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top