|
It works! You were right, the function is const.
Just two things I don't get, though:
1) Why does the iterator must be constant too?
2) Why do you say that making the function not const is not good? I'm asking this because I've seen different opinions in this matter. On one hand, they would argue that making a function const allows the compiler to optimize the function in several ways. On the other hand, I remember reading an article that said that "marking a function const is only a guarantee you're giving to the user that the method will not change the class's attributes" (nor its input, I think...). What do you think?
Regards
Fratelli
|
|
|
|
|
AndreFratelli wrote: 2) Why do you say that making the function not const is not good?
- When marking a member function as const. You're telling the user of the class that this call to will not change the internal data of the class. This is 'guaranteed'.
- You're alsoo telling the compiler to help you the writer too fulfill this design requirement. The compiler will modify for example a member int m_x; to const int m_x;
AndreFratelli wrote: 1) Why does the iterator must be constant too?
- The compiler has modified the constantness of this member, meaning that this member cannot be changed. Hence you need to use the const version of the iterator;
Learn from the mistakes of others, you may not live long enough to make them all yourself.
|
|
|
|
|
All clear now =D
Thanks a lot !
Best regards
Fratelli
|
|
|
|
|
Glad I could help
Learn from the mistakes of others, you may not live long enough to make them all yourself.
|
|
|
|
|
AndreFratelli wrote: std::vector<std::vector xmlns:std="#unknown"><ligacao> >
Just a suggestion!
Use a typedef to create type for this vector and will make things easier for you...
typedef std::vector<std::vector<Ligacao *> > LigacaoVector2d; Then create iterators like
LigacaoVector2d::iterator it;
Nibu babu thomas
Microsoft MVP for VC++
Code must be written to be read, not by the compiler, but by another human being.
Programming Blog: http://nibuthomas.wordpress.com
|
|
|
|
|
Yes, I usually do that.. But when dealing with arrays I also define an "iterator" type, like:
typedef std::vector<std::vector<Ligacao *> > LigacaoVector2d;
typeded LigacaoVector2d::iterator iterator;
That way, I can even do:
iterator it;
Thx anyway =)
best regards
Fratelli
|
|
|
|
|
AndreFratelli wrote: typeded LigacaoVector2d::iterator iterator;
It's better to use it directly so that readers of your code can understand from where this iterator is being used/to which vector this iterator belongs. Isn't it?
Nibu babu thomas
Microsoft MVP for VC++
Code must be written to be read, not by the compiler, but by another human being.
Programming Blog: http://nibuthomas.wordpress.com
|
|
|
|
|
True =) but, on the other hand, if you have a class with the following definitions:
template <typename T> class MyClass
{
typedef vector<T> my_vector;
typedef my_vector::iterator iterator;
}; Then you/others can do something like:
int main()
{
MyClass<type>::iterator it;
return 0;
} Instead of:
int main()
{
MyClass<type>::my_vector::iterator it;
} Would you agree that it is practical?
regards
Fratelli
|
|
|
|
|
AndreFratelli wrote: Would you agree that it is practical?
Yeah, I thought you were letting the iterator typedef float around, if it's in a class then should be fine.
Nibu babu thomas
Microsoft MVP for VC++
Code must be written to be read, not by the compiler, but by another human being.
Programming Blog: http://nibuthomas.wordpress.com
|
|
|
|
|
I'm writing a windows socket wrapper class in c++ from scratch and i cannot get events to work.
So inside the class i'll have several events.
let's pretend i have the following class:
class SocketTree
{
public:
int Close();
virtual void OnClose();
}
SocketTree::Close()
{
OnClose();
return 0;
}
And now i'll have the derived class that will receive the event:
class SckHandle : public SocketTree
{
public:
virtual void OnClose();
}
SckHandle::OnClose()
{
printf("working.");
}
And here's the main procedure:
int main()
{
SckHandle sHnd;
sHnd.Close();
return 0;
}
when sHand.Close() is executed, i wanted the event OnClose() to be fired, but it won't... anyone knows what is happening in here and what should i do to handle events correctly?
thank you 
|
|
|
|
|
should give a linker error -- SocketTree::OnClose was not defined...if you define it, it will probably work properly. If it is not your intention to have a valid OnClose for SocketTree, then you could declare it as pure virtual by putting a "= 0;" at the end. But then the class is pure virtual, so you can't instantiate objects of that type, only derived objects. Technically speaking, you should also have a "using SocketTree::Close()" as part of your derived class's public area if you intend to use base class functions from your derived objects. Most people probably wouldn't do that I'm guessing, but it is more accurate.
|
|
|
|
|
I fully agree with the above reply. Just one update. The brackets are not allowed with the "using" keyword. using SocketTree::Close; is Ok. Normally this statement is not required.
|
|
|
|
|
Dig stepwise with Debugger in it and you will see that you call SocketTree::OnClose().
Write in class SocketTree:
virtual void OnClose() = 0;
Greetings from Germany
|
|
|
|
|
Hi,
I have create a win32 console application. if I run that application, it executes and dissappears. To avoide this i used getch() to hold the application after execution.
Now i don want to use getch(). Insted i would like to use batch file. I few doubts in creating batch file.
1) As this is command line exe..it take few arguments(where user can give the arguemts as he required.). So is it possible for a batch file to take this argument and execute the application with that argument.
2) Is there any other way to deal with this.
Thanks,
Nandu
|
|
|
|
|
Batch files can take up arguments, can run other executables, and can do a lot more perverted things. But my gut feeling is that you should be able to do what you are looking to do without a batch. However, look at the following URL for a batch file tutorial:
http://snipr.com/2uadw[^]
|
|
|
|
|
Hi,
I am working for a project as per the client requirement. We wrote a win32 console application (command line exe). While executing this application its runs and disappears to avoided this I used getch(). But I don’t know the reason, client don’t want to use this getch(). He recommended using batch file. I posted the same question earlier as well, some people suggested to use system("pause"). I don’t the difference between getch() and system("pause"). So I doubt whether client will accept using
system("pause") since he recommended to use batch file.
Which is the better solution here using batch file or system("pause")?
Thanks
Mahadevan
|
|
|
|
|
Why should the application not disappear after it is done? If it should not disappear, why does your client not want you to use getch()?
|
|
|
|
|
As I told you I dont know the reason why we should not use getch(). Is using getch() and system("pause") is same?
|
|
|
|
|
Nandu_77b wrote: Is using getch() and system("pause") is same?
getch() is a Posix[^] function and I shall suggest you to use it or getc() or getchar() . On the other hand, system("pause") is a nasty thing to do, it is a very resource expensive call and it is not POSIX complaint (which means you cannot port it to another operating system).
If you could explain your need better, somebody over here shall give you an appropriate answer. But, avoid the system() call by all means.
|
|
|
|
|
Thanks a lot,
As batch flie helps and its very simple, I am going with batch file rather than getch() or system("pause").
Below thread shows the batch file impl
-Nandu
|
|
|
|
|
Hi
1) Yes, it is possible to pass arguments to a batch file. You use %number to get the argument by counting. For instance:
REM batch.bat
echo %1
echo %2
REM batch.bat
Now you call it "batch.bat Hello World" and the output is
Hello
World
If you have "echo off".
2) A better one =) I call the application from the prompt line, instead of just clicking it. This way, when it finishes, the window is still open and I can see the output.
regards
|
|
|
|
|
Below is the batch file I created. This batch file is used to run a command line exe which takes some parameters. Most of the time, it works with two parameters one for input file and the other for output file.
For example
C:\>myapp.exe input.txt output.txt.
I am just doing the execution using a below batch file.
REM xxx.bat
@echo OFF
%1
%2
%3
%4
myapp.exe %*
pause
But while executing batch file
xxx.bat input.txt output.txt
It opens the input.txt file and after closing the input.txt file it starts executing the myapp.exe with the given paramters.
I don know how to stop opening the input.txt, so that it will not open the input.txt and rather just executes the myapp.exe with the given parameters. Can you please help me to stop opening the input.txt file while executing the batch file.
Thanks,
Nandu
|
|
|
|
|
Sorry this question and just a general reply, in the above i forgotten to mark as question.
Below is the batch file I created. This batch file is used to run a command line exe which takes some parameters. Most of the time, it works with two parameters one for input file and the other for output file.
For example
C:\>myapp.exe input.txt output.txt.
I am just doing the execution using a below batch file.
REM xxx.bat
@echo OFF
%1
%2
%3
%4
myapp.exe %*
pause
But while executing batch file
xxx.bat input.txt output.txt
It opens the input.txt file and after closing the input.txt file it starts executing the myapp.exe with the given paramters.
I don know how to stop opening the input.txt, so that it will not open the input.txt and rather just executes the myapp.exe with the given parameters. Can you please help me to stop opening the input.txt file while executing the batch file.
Thanks,
Nandu
|
|
|
|
|
Thanks,
I got the solution, I have use ECHO as shown below.
Forum: Visual C++ / MFC
Subject: Re: About batch file
Sender: Nandu_77b
Date: Monday, July 7, 2008 1:14 AM
Sorry this question and just a general reply, in the above i forgotten to mark as question.
Below is the batch file I created. This batch file is used to run a command line exe which takes some parameters. Most of the time, it works with two parameters one for input file and the other for output file.
For example
C:\>myapp.exe input.txt output.txt.
I am just doing the execution using a below batch file.
REM xxx.bat
@echo OFF
ECHO %1
ECHO %2
ECHO %3
ECHO %4
myapp.exe %*
pause
-Nandu
|
|
|
|
|
It opens the file because of these lines:
%1
%2
%3
%4
You should write only:
REM xxx.bat
@echo OFF
myapp.exe %*
pause
If %1 is equal to "input.txt", them writing %1 just like that will open input.txt with the default application (usually notepad).
Hope i'v helped =)
Fratelli
|
|
|
|