|
|
|
|
bot(char *_server, char *_port);
which it will no longer generate a default one automatically. This might cause you problems in the long run that can be painful to track down.@Zereo I would like to know what problems could occur from not having a default constructor. There are millions of C++ classes that could not work at all with a default constructor. |
int status, result;
+1 to @L B. There are many reasons to not have a default constructor, and this certainly could be one of them. |
Zereo wrote: |
---|
1) Well for this example since he doesn't have a default constructor these two variables are undefined and could lead to undefined behaviorint status, result; |
Zereo wrote: |
---|
2) Also without a default constructor he will error out if he tries to do something like this bot myBot; . |
Zereo wrote: |
---|
3) Since he has no default constructor he can't use any containers with his class type since the copy constructor needs the default constructor to create the temporary object. |
Zereo wrote: |
---|
There are a lot of issues/bugs that can arise and it also significantly reduces the classes usability to not have a default constructor. |
Zereo wrote: |
---|
Could you please name one instance where you would not want a default constructor? I am just curious and I could be wrong but I don't see any reason why you would not have a default constructor either implicitly or explicitly. Like I said I could be wrong but I see almost no use of not having a default constructor... |
Member variables are alwways initialized via their default constructor if the class constructor being invoked does not initialize them otherwise. In this case, int's default ctor initalizes them to 0 |
That is the purpose of a default constructor - some classes need to have parameters passed to them to be constructed. |
bot(char *_server, char *_port);
and never explicitly declared a default constructor bot();
so he doesn't have one. I realize some classes could use parameters passed to them to construct them by why not declare that AND a default ctor?The default constructor is not related to the copy constructor, and even so I don't think he needs STL container support. If he did, he could add the required member functions. |
vector<string> svec(5);
for example. The compiler first initializes svec by using the default string constructor to create a temporary value. Then the copy constructor is used to copy the temporary into each element of svec. So how does the copy constructor not use the default constructor?You are mislead. |
std::istream and std::ostream have no default constructor. |
std::ofstream out;
or std::istream in;
? Would that be calling the default constructor?1) Well for this example since he doesn't have a default constructor these two variables are undefined and could lead to undefined behavior |
2) Every constructor for every class that uses his class type will need to explicitly initialize the class members for his class. |
3) Classes that have members of bot type will not implicitly generate a default constructor for there class so they to would also have to explicitly define a default constructor and have that constructor explicitly initialize bot type's members |
4) His class can not be used as a element in a dynamically allocated array. If the array is statically allocated it must define a explicit initializer for each element. |
5) These might be me misunderstand some things but I believe he can only pass and return type bot to/from a function by only reference since the copy constructor requires a default constructor to make the temporary objects. Also I believe some containers like vectors will be out of the question also since I believe they use the copy constructor which uses the default constructor to make the elements. But like I said this might be incorrect or me misunderstand how the copy constructor works. |
Could you please name one instance where you would not want a default constructor? I am just curious and I could be wrong but I don't see any reason why you would not have a default constructor either implicitly or explicitly. |
Zereo wrote: |
---|
umm... int doesn't have a default constructor it is a integral type.. .It would be initialized to 0 if it was in global scope, and undefined if it is in local scope. |
|
|
Zereo wrote: |
---|
Also that brings another reason to the table like you said member variable of class types are always initialized via their default constructor if they are not explicitly initialized so that could lead to errors by users of the class. |
|
|
Zereo wrote: |
---|
I don't think you get what I said... He doesn't have a default constructor... |
Zereo wrote: |
---|
I realize some classes could use parameters passed to them to construct them by why not declare that AND a default ctor? |
Zereo wrote: |
---|
??? Take vector<string> svec(5); for example. The compiler first initializes svec by using the default string constructor to create a temporary value. Then the copy constructor is used to copy the temporary into each element of svec. So how does the copy constructor not use the default constructor? |
|
|
Zereo wrote: |
---|
I can't see any situation where it would hurt the program to have one though there probably is some situations. |
Zereo wrote: |
---|
How would you be able to do this std::ofstream out; or std::istream in; ? Would that be calling the default constructor? |
Zereo wrote: |
---|
I wasn't trying to get into a big debate about constructors I was just letting the OP know that he doesn't have a default constructor in the code he posted, and that might not be what he wanted. |
Primitive types do have constructors, and on top of that they do have default constructors: |
|
|
Users of the class are not responsible for initializing the class members, this is the responsibility of the class constructor. |
|
|
No, the elements are constructed in-place and the copy constructor is never called. However, it is perfectly possible to use std::vector with a class that has no default constructor. All you have to do is not using any functions of std::vector which require a default ctor, and the code will work. |
|
|
You seem to have been arguing that not having a default constructor is harmful to the program; this is where I claim you are mislead. |
I am just trying to make sure that neither you nor the OP is confused about the implications of having or not having a default constructor. |
I use classes all the time with reference members. As @LB said, they never have a default constructor. In this case, the OPer wants his bot constructor to connect to a server and port. That make perfect sense to me. The constructor can't do that if it has defaulted values for host and port. This is a GOOD reason to not have a default constructor. |
Right. Since a bot will connect upon construction, it needs to be initialized. Containing objects will have to initialize the bots upon their construction. If that's the desired behavior, then that's perfectly fine. I think this make good sense in this situation. |
Zereo wrote: |
---|
Tell me what behavior you get? Is notGood 0? |
Zereo wrote: |
---|
If he doesn't have a default a default constructor and the user uses his class type in a class of his own and assumes that it has a default constructor it will lead to a frustrating error. |
Zereo wrote: |
---|
You are wrong with the example I gave you it uses the default constructor to make the temporary object and then uses the copy constructor to copy it into the vector. |
Zereo wrote: |
---|
For the average program yes it can be harmful to not have a default constructor. |
Zereo wrote: |
---|
I am going off of what I have learned from Effective C++ and C++ Primer and most of the cases that I listed are coming right out of the book. So i'm going with their word on it =/. |
Both outputs are 0. Did you even test this? Proof: http://ideone.com/tITNKY |
It is his fault for assuming that is has a default constructor, and it is a simple fix as well given that they should be able to understand the compiler error. |
The only harm is making the assumption that a type has a default ctor. Not having a default ctor does not cause any harm. |
Which C++ Primer? There is a good one and a bad one. Also, could you provide page numbers and copy the exact argument/examples they give? EDIT: Oh, and which edition of Effective C++? |
Zereo wrote: |
---|
Umm did you? Because both codeblocks and VS2010 are giving undefined behavior for cout << wontWork.notGood << endl;. I would retry that a non online compiler. |
Zereo wrote: |
---|
Yes it is but classes are supposed to be made to be as easy for the user(class user) to use as possible, meaning not limiting them on whatever they want to do as long as it is reasonable. |
int i(0)
notation is syntactic sugar (and nesescary for init lists)
|
|