473,325 Members | 2,442 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,325 software developers and data experts.

access private member function through a function having template type

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 Tvoid 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

Dec 28 '06 #1
9 2724

"Mike" <go*********@gmail.comwrote in message
news:11**********************@n51g2000cwc.googlegr oups.com...
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 Tvoid 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
Dec 28 '06 #2
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++"

Mike Wahler wrote:
"Mike" <go*********@gmail.comwrote in message
news:11**********************@n51g2000cwc.googlegr oups.com...
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 Tvoid 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
Dec 28 '06 #3

Mike wrote:
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 Tvoid 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?

Dec 28 '06 #4

Mike Wahler wrote:
"Mike" <go*********@gmail.comwrote in message
news:11**********************@n51g2000cwc.googlegr oups.com...
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 Tvoid 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'"
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 ?

>
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
Dec 28 '06 #5

Yahooooooooo wrote:
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
>
spent half a day to know this when i was learning...C++

:)

Regards,
M.Azmath
"Evergreen C++"

Mike Wahler wrote:
"Mike" <go*********@gmail.comwrote in message
news:11**********************@n51g2000cwc.googlegr oups.com...
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 Tvoid 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
Dec 28 '06 #6

gangs wrote:
Mike wrote:
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 Tvoid 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?
I used g++(3.2.2) on red hat linux.

Dec 28 '06 #7

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 Tclass A{ T t[100000]; };
template<class Tclass B{ T t[100000]; };
template<class Tclass C{ T t[100000]; };
template<class Tclass D{ T t[100000]; };
template<class Tclass E{ T t[100000]; };
// ..... etc. ....(skip class T{};)
template<class Tclass Z{ T t[100000]; };

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

How much did the executable grow?
Then change main:

int main(){
A<intAint;
B<intBint;
// ... etc.
Z<intZint;
std::cout<<"Hello World";
}

Now how much did the executable grow?

--
Bob R
POVrookie
Dec 28 '06 #8
Mike wrote:
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 Tvoid 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 Tvoid 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

Dec 29 '06 #9

Taran wrote:
Mike wrote:
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 Tvoid 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.
Good. That makes sense. Thanks.

>
#include<iostream>
using namespace std;
class Testable {
void pp() {} // a private function
};
template <typename Tvoid 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
Dec 29 '06 #10

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

Similar topics

11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
12
by: James Brown | last post by:
Hi all, Having problems designing a template-class. I'll describe my scenario first then show what I've come up with so far: Need a class to provide pointer/array-like access to an area of...
2
by: James Brown | last post by:
Hi again, I'm referring back to my previous posting of the same title, with everyone's help I've now got a better understanding of what my goals are and I have a class which now looks like: ...
9
by: Maciej | last post by:
Hi, I tried to build windl project from the book 'The Art of C++" in VS.NET 7 and it fails. I attached all required .lib files. I got the following error messages from the linker: windl error...
3
by: Random Person | last post by:
Does anyone know how to use VBA to relink tables between two MS Access databases? We have two databases, one with VBA code and the other with data tables. The tables are referenced by linked...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
23
by: Ben Voigt | last post by:
I have a POD type with a private destructor. There are a whole hierarchy of derived POD types, all meant to be freed using a public member function Destroy in the base class. I get warning C4624....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.