access private member function through a function having template type

M

Mike

Hi,

Just a simple question: why the compiler doesn't report error when
accessing a private member function inside a function having template
type ?

For example:

#include<iostream>
using namespace std;
class Testable {
void pp() {} // a private function
};

template <typename T> void goo(const Testable& lhs, const T& rhs) {
lhs.pp(); // it is ok ??
}

void goo2(const Testable& lhs) {
lhs.pp(); // the compiler reports error.
}

Why does the compiler not emit error in function "goo" ?

Thanks.

Mike
 
M

Mike Wahler

Mike said:
Hi,

Just a simple question: why the compiler doesn't report error when
accessing a private member function inside a function having template
type ?

For example:

#include<iostream>
using namespace std;
class Testable {
void pp() {} // a private function
};

template <typename T> void goo(const Testable& lhs, const T& rhs) {
lhs.pp(); // it is ok ??
}

void goo2(const Testable& lhs) {
lhs.pp(); // the compiler reports error.
}

Why does the compiler not emit error in function "goo" ?

Because if a templated function is never called, no code is created
for it. Try calling it, and you should get an error to the
effect of: "cannot access private member declared in class 'Testable'"

Note that first you'll need to declare 'Testable::pp()' as a const
member function, or change 'goo()'s parameter 'lhs' to nonconst;
otherwise you'll also get an error about "cannot convert 'this' pointer
from 'const Testable' to 'Testable &'"

-Mike
 
Y

Yahooooooooo

Before accessing private member fucntion of a class , can you please
read what the PRIVATE word implies...

You cannot access private members fuctions other than same class PUBLIC
member functions.
So no question of private members functions access from template
fuctnions.

spent half a day to know this when i was learning...C++

:)

Regards,
M.Azmath
"Evergreen C++"
 
G

gangs

Mike said:
Hi,

Just a simple question: why the compiler doesn't report error when
accessing a private member function inside a function having template
type ?

For example:

#include<iostream>
using namespace std;
class Testable {
void pp() {} // a private function
};

template <typename T> void goo(const Testable& lhs, const T& rhs) {
lhs.pp(); // it is ok ??
}

void goo2(const Testable& lhs) {
lhs.pp(); // the compiler reports error.
}

Why does the compiler not emit error in function "goo" ?

Thanks.

Mike

Certainly not compiling for me even in the templatized version. I used
VC6 and DevC++ 4.9.9.2 (using mingw32 gcc version 3.4.2 ), in both
cases I got the error at compilation time.
Which compiler are you using?
 
M

Mike

Mike said:
Because if a templated function is never called, no code is created
for it. Try calling it, and you should get an error to the
effect of: "cannot access private member declared in class 'Testable'"

Thanks for answering.

Yes. The error message does show when I use "goo". Then I am
interested in how the compiler does it.

I guess the compiler seems to tolerate the "lhs.pp()" when scanning the
function body of "goo". But when it sees the invocation of "goo", it
goes back and does the semantic checking. If that's the case, is it
inefficient ?
 
M

Mike

Yahooooooooo said:
Before accessing private member fucntion of a class , can you please
read what the PRIVATE word implies...

You cannot access private members fuctions other than same class PUBLIC
member functions.
So no question of private members functions access from template
fuctnions.

Hi, Yahooooooooo,

I don't think your answer is correct. Please see Mike Wahler 's message
above.

Regards,

Mike
 
M

Mike

gangs said:
Certainly not compiling for me even in the templatized version. I used
VC6 and DevC++ 4.9.9.2 (using mingw32 gcc version 3.4.2 ), in both
cases I got the error at compilation time.
Which compiler are you using?

I used g++(3.2.2) on red hat linux.
 
B

BobR

Mike wrote in message...
I guess the compiler seems to tolerate the "lhs.pp()" when scanning the
function body of "goo". But when it sees the invocation of "goo", it
goes back and does the semantic checking. If that's the case, is it
inefficient ?

You are trying to compare a blueprint of a house (the class template) with a
house (the instantiated class).

Make a program:

#include <iostream>
int main(){
std::cout<<"Hello World";
}

Compile and look at the executable's size. Write it down.
Now do this:

#include <iostream>
template<class T> class A{ T t[100000]; };
template<class T> class B{ T t[100000]; };
template<class T> class C{ T t[100000]; };
template<class T> class D{ T t[100000]; };
template<class T> class E{ T t[100000]; };
// ..... etc. ....(skip class T{};)
template<class T> class Z{ T t[100000]; };

int main(){
std::cout<<"Hello World";
}

How much did the executable grow?
Then change main:

int main(){
A<int> Aint;
B<int> Bint;
// ... etc.
Z<int> Zint;
std::cout<<"Hello World";
}

Now how much did the executable grow?
 
T

Taran

Mike said:
Hi,

Just a simple question: why the compiler doesn't report error when
accessing a private member function inside a function having template
type ?

For example:

#include<iostream>
using namespace std;
class Testable {
void pp() {} // a private function
};

template <typename T> void goo(const Testable& lhs, const T& rhs) {
lhs.pp(); // it is ok ??
}

void goo2(const Testable& lhs) {
lhs.pp(); // the compiler reports error.
}

Why does the compiler not emit error in function "goo" ?

Thanks.

Mike

Templates are like preprocessor macros. During preprocessing the
template code is intantiated for each type of the template type, this
is called instantitation. So even if you have a template which has a
erronoeus code, as you have in here. the compiler will not complain if
your template is not intantiated. Simple put non instantiated template
code does not exist.
#include<iostream>
using namespace std;
class Testable {
void pp() {} // a private function
};
template <typename T> void goo(const Testable& lhs, const T& rhs) {
lhs.pp(); // it is ok ??
}
The compier will complain only when you instantiate this template
function
E.g

void goo<int>(Testbale_object, some_inv_value);
//The compiler will complain now as the template with error has been
intantiated.

void goo2(const Testable& lhs) {
lhs.pp(); // the compiler reports error.
}
Expected.

Why does the compiler not emit error in function "goo" ?

The template was instantiated and hence the code does not exist during
compilation.
A word of caution, since templates are intantiated for each type you
specify, this is a cause of code bloat. You may to read more about
this.

- HTH

Regards,
Taran
 
M

Mike

Taran said:
Templates are like preprocessor macros. During preprocessing the
template code is intantiated for each type of the template type, this
is called instantitation. So even if you have a template which has a
erronoeus code, as you have in here. the compiler will not complain if
your template is not intantiated. Simple put non instantiated template
code does not exist.

Good. That makes sense. Thanks.
 

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,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top