Ah, nevermind. It had to do with the way I was doing the function. I'm not sure what I was doing but obviously it was wrong and I am a VERY BAD BOY.
Anyway though, sort of derailing this, how do I compile/create a multi-source project, again?
Printable View
Ah, nevermind. It had to do with the way I was doing the function. I'm not sure what I was doing but obviously it was wrong and I am a VERY BAD BOY.
Anyway though, sort of derailing this, how do I compile/create a multi-source project, again?
Mm, well, what compiler/IDE/whatever are you using? In MSVC you just add a new .cpp file to your project and a .h file to put your function prototypes in, include the .h file in main.cpp, and it will all be built together.
Dev-C++... Look a few posts back. ;)
Anyway, you can make projects in Dev, and do essentially the same thing as MSVC.
Another way, compile (do not build) all of your source files individually, and then build the project (this will link the object files). I don't use Dev (or Windows for that matter) anymore, so I can't give you much more than that.
It's called an "initializer list". The following is a simple example.Quote:
Originally Posted by Hunter2
No initializer list, simple assignment within constructor body:
Initializer list:Code:class A
{
public:
A(int i1, int i2, int i3)
{
a = i1;
b = i2;
c = i3;
}
int get_a()
{
return a;
}
private:
int a;
int b;
int c;
};
The code:Code:class A
{
public:
A(int i1, int i2, int i3): a(i1), b(i2), c(i3)
{
}
int get_a()
{
return a;
}
private:
int a;
int b;
int c;
};
: a(i1), b(i2), c(i3)
says to set a equal to i1, set b equal to i2, and set c equal to i3.
Initializer lists can be more efficient because they initialize the member as it's being created, rather than creating the member and then performing an assignment, and in some cases an initializer list is the only way to set values for certain types of members.
>>No initializer list, simple assignment within constructor body:
Yes, for POD types. But if you have a class with no copy constructor or = operator defined, or no default constructor either, then you MUST do the initialization in the initializer list.
See above:Quote:
>>No initializer list, simple assignment within constructor body:
Yes, for POD types. But if you have a class with no copy constructor or = operator defined, or no default constructor either, then you MUST do the initialization in the initializer list.
Does the code I posted produce errors? I wonder if you got half of your 2,000 posts with responses like that? In addition, you might want to try to learn to speak to your audience.Quote:
...and in some cases an initializer list is the only way to set values for certain types of members.
Also, the initializer list is the proper place to call base class ctors.Quote:
Originally Posted by Hunter2
Dev-C++ doesn't handle projects right for some reason. It doesn't recognize any changes I make to the included headers and whatnot. Yes, I AM saving them between compiles. I'm thinking it's not making new objects (Is that what you get from the first compile stage?) and just tossing in the old ones or something.
>>and in some cases an initializer list is the only way to set values for certain types of members.
Didn't see that.
>>Does the code I posted produce errors?
No, but the object of my post was to point out that even though the results may be the same, using the initializer list is in no way equivalent to an assignment.
>>I wonder if you got half of your 2,000 posts with responses like that?
Practice what you preach. Your own post is little more than a restatement of my previous post, and less accurate and in-depth at that.
>>In addition, you might want to try to learn to speak to your audience.
My posts are for everybody to see; I simply added to (clarified?) your post. On the other hand, you've just contradicted yourself by dedicating an entire post to 'defending' yourself and insulting me, even though I did nothing deserving of your flames.
Where did you say that? Here is your entire post:Quote:
Originally Posted by Hunter2
You never even used the word "assignment". Once again, compare that to what I posted:Quote:
>>No initializer list, simple assignment within constructor body:
Yes, for POD types. But if you have a class with no copy constructor or = operator defined, or no default constructor either, then you MUST do the initialization in the initializer list.
====Quote:
Initializer lists can be more efficient because they initialize the member as it's being created, rather than creating the member and then performing an assignment, and in some cases an initializer list is the only way to set values for certain types of members.
I didn't notice where you identified the code as an initializer list, nor explained in simple terms what it does. This is what I saw:Quote:
Originally Posted by Hunter2
----Quote:
The constructor of B takes an A*, and we'll name it 'parent'. Now, when B's constructor is called, i.e. it's being constructed, it will construct member_parent which is an A* by passing the A* constructor 'parent'. Since a pointer is a POD type, the 'constructor' of A* is automatically created, and all it does is copy the value passed to it into itself. So what happens is the value stored in 'parent' is copied into member_parent when B's constructor is called.
If you are having trouble with porjects (from my past experience, it handles them well, but I don't have Windows to use it on, so I can't directly help you troubleshoot), then you can compile then individually. Simply compile -- but do not build -- all of your source files. Then build the project. Assuming they are all in the same directory, you should not have problems. If you think it is not properly recreating your object files, simply go into the folder, delete them, and then perform your compilations, making sure you are compiling the correct copy of the source files.Quote:
Originally Posted by suzakugaiden
Cheers
>>I didn't notice where you identified the code as an initializer list
I did not identify it as an initializer list very simply because I did not know the correct terminology; I did not dispute that you added this information to the discussion. Because I realized that my explanation was somewhat confusing and difficult to follow, I added a code example that hopefully explained better; and above all, my information was accurate.
>>rather than creating the member and then performing an assignment
True, but the first 95% of your post seems to equate the initializer list with assignment, and only in the last line do you vaguely suggest that there is a difference - and then instead of stressing the greater significance of the distinction, all you say is that there is a performance gain due to the fact that the value is given during creation rather than the object being created and then assigned a value later!
>>You never even used the word "assignment".
I'd assumed that my comment would prompt further investigation into the matter, and the repeated use of 'constructor' would link back to my own explanation where I explained exactly what initializer lists do and how - and therefore the distinction would be made, albeit somewhat indirectly.