I wasn't sure how to describe this situation so I don't know what to search for on the web.
I have two classes, A and B. One of the constructors for B takes a class A as a parameter. This works fine, but it is annoying because I have to create a class A object and pass it as a parameter.
1 2
A MyTemp (2, 14);
B MyObject (Blah, MyTemp);
Is there a way I can make class A on the fly and hand it to class B like this? B MyObject (Blah, A(2, 14));
Obviously, that doesn't work. It gives the error "function-style cast".
But, I hope you see the point about making the class on the fly. I was thinking that maybe a function would do but I think that that would be a bit redundant if there is another solution.
That should work. The following code should compile nicely:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
class A{
int i;
public:
A(int I) : i(I){}
};
class B{
int i;
A a;
public:
B(int I, A alpha) : i(I), a(alpha){}
};
int main(){
B beta(5, A(7));
return 0;
}
Hmm, the error must be actually the code I lack. I don't know what the ": i(I)" or " A(int I) : i(I)" code is, I haven't seen it before. Could you explain what it is?
These are irrelevant. They are initialiser lists. This is a way to call the constructors of members. This could be a little bit quicker, since otherwise default constructors are called. It makes no difference for primitive data types though.
1 2
A(int I) : i(I){
}
is eqivalent to
1 2 3
A(int I){
i = I;
}
However if you want to do this with B's constructor, you'll have to write a default constructor for A.
1 2 3 4
B(int I, A alpha){//the default constructor of A will be called here.
i = I;
a = alpha;
}
I can define an image OK like this: Image MyPicture;
But when I try to do this, which you say compiles fine, I get the error. Image MyPicture (Vect(100, 100));
This code SHOULD create an image with a size 100 by 100.
Also, I am still learning and I will change and optimize my classes, I know there is a lot of stuff I could be doing better.
This also leads to my other question; you will notice that in a couple of the classes one of the constructors uses a bool DUMMY parameter. I did this because the compiler gives me an error if the first parameter is the same type as the class itself. It works as the second paremeter. Why won't it let me use it as the first parameter?
I had no problems with that (Except that you should add usingnamespace std; or change each vector<...> to std::vector<...>).
A constructor that takes object of the same class is a special case - the copy constructor. It's Myclass(Myclass&) or Myclass(const Myclass&). I suppose Myclass(Myclass) would conflict with the copy constructor and therefore is not allowed.
Thanks for pointing out the usingnamespace std; bit, I actually had that above the include for my header.
Anyway, I really don't see why it just does not let me compile that! I'm using Visual C++ 2008, if that makes any difference. I even tried in a new, clean project and it still gives the same three errors every time:
I'm so sorry everyone! It seems that this code Image MyImage (Vect(100, 100)); can't be done inside a class, struct, or union. When I move the code outside of it, it compiles without a problem. I probably should have mentioned where I was declaring it...