 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
John Fly Guest
|
Posted: Wed Sep 14, 2005 7:20 am Post subject: std::<vector> of base class issues.... Why I can't call memb |
|
|
I have a base log class cLog... I'm storeing inhereted "logs" in a
vector of log pointers so I can send generic messages to every log and
have it write out in its own unique format...
Right now to access my member functions I'm having to create a real
pointer, why can't I just use the iterator?
void cLogServer::LogMsg(MessageLevel Level, std::string& Message)
{
std::vector<cLog*>::iterator iLogs = logs_.begin();
std::vector<cLog*>::iterator iLogsEnd = logs_.end();
for(;iLogs != iLogsEnd; iLogs++)
{
cLog* test = *iLogs; <---- Why do I have to use
this
test->Write(INFO, Message);
}
}
I want to use *iLogs->Write(INFO, Message) or something similar.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Wed Sep 14, 2005 9:45 am Post subject: Re: std::<vector> of base class issues.... Why I can't call |
|
|
John Fly wrote:
| Quote: | std::vector<cLog*>::iterator iLogs = logs_.begin();
cLog* test = *iLogs; <---- Why do I have to use this
I want to use *iLogs->Write(INFO, Message) or something similar.
|
The last snippet is evaluated like this:
*(iLogs->Write(INFO, Message))
but you want
(*iLogs)->Write(INFO, Message)
which is exactly what you should write instead.
Uli
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Tony Delroy Guest
|
Posted: Wed Sep 14, 2005 9:48 am Post subject: Re: std::<vector> of base class issues.... Why I can't call |
|
|
-> has higher precendence than *, so *iLogs->Write(...) tries to
dereference the result of calling the iterator's Write() function. Try
(*iLogs)->Write(...). - Tony
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ramashish Guest
|
Posted: Wed Sep 14, 2005 9:52 am Post subject: Re: std::<vector> of base class issues.... Why I can't call |
|
|
Hi John,
| Quote: |
Right now to access my member functions I'm having to create a real
pointer, why can't I just use the iterator?
void cLogServer::LogMsg(MessageLevel Level, std::string& Message)
{
std::vector<cLog*>::iterator iLogs = logs_.begin();
std::vector<cLog*>::iterator iLogsEnd = logs_.end();
for(;iLogs != iLogsEnd; iLogs++)
{
cLog* test = *iLogs; <---- Why do I have to use
this
test->Write(INFO, Message);
}
}
I want to use *iLogs->Write(INFO, Message) or something similar.
|
The precedence of -> is higher than *, so you should say
(*iLogs)->Write(INFO, Message);
Regards
Ram
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Boris Bralo Guest
|
Posted: Wed Sep 14, 2005 9:53 am Post subject: Re: std::<vector> of base class issues.... Why I can't call |
|
|
Hi john,
| Quote: |
for(;iLogs != iLogsEnd; iLogs++)
{
cLog* test = *iLogs; <---- Why do I have to use
this
test->Write(INFO, Message);
I want to use *iLogs->Write(INFO, Message) or something similar.
Correct statement is: |
(*iLogs)->Write(INFO, Message).
(operator precedence rules).
Boris
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Bo Persson Guest
|
Posted: Wed Sep 14, 2005 9:53 am Post subject: Re: std::<vector> of base class issues.... Why I can't call |
|
|
"John Fly" <JohnFlyTn (AT) msn (DOT) com> skrev i meddelandet
news:1126635864.859846.107040 (AT) g49g2000cwa (DOT) googlegroups.com...
| Quote: | I have a base log class cLog... I'm storeing inhereted "logs" in a
vector of log pointers so I can send generic messages to every log
and
have it write out in its own unique format...
Right now to access my member functions I'm having to create a real
pointer, why can't I just use the iterator?
void cLogServer::LogMsg(MessageLevel Level, std::string& Message)
{
std::vector<cLog*>::iterator iLogs = logs_.begin();
std::vector<cLog*>::iterator iLogsEnd = logs_.end();
for(;iLogs != iLogsEnd; iLogs++)
{
cLog* test = *iLogs; <---- Why do I have to
use
this
test->Write(INFO, Message);
}
}
I want to use *iLogs->Write(INFO, Message) or something similar.
|
It is a matter of operator precedence. Here operator -> binds before
operator *, so in fact this is equivalent to
*(iLogs->Write(INFO, Message))
which obviously isn't what you want. You should try
(*iLogs)->Write(INFO, Message)
instead.
Bo Persson
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|