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 9 2687
"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
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
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?
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
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
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.
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
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
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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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:
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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....
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: kcodez |
last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: lllomh |
last post by:
How does React native implement an English player?
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |