C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

std::<vector> of base class issues.... Why I can't call memb

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
John Fly
Guest





PostPosted: Wed Sep 14, 2005 7:20 am    Post subject: std::<vector> of base class issues.... Why I can't call memb Reply with 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.


[ 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





PostPosted: Wed Sep 14, 2005 9:45 am    Post subject: Re: std::<vector> of base class issues.... Why I can't call Reply with quote



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





PostPosted: Wed Sep 14, 2005 9:48 am    Post subject: Re: std::<vector> of base class issues.... Why I can't call Reply with quote



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





PostPosted: Wed Sep 14, 2005 9:52 am    Post subject: Re: std::<vector> of base class issues.... Why I can't call Reply with quote

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





PostPosted: Wed Sep 14, 2005 9:53 am    Post subject: Re: std::<vector> of base class issues.... Why I can't call Reply with quote

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





PostPosted: Wed Sep 14, 2005 9:53 am    Post subject: Re: std::<vector> of base class issues.... Why I can't call Reply with quote


"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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.