Condition& source; // not initiated - produces error
Effect& source_; // not initiated
// I cannot initiate the source and source_ because I don't know the type yet
if (data->type == EFFECT)
std::swap(source, source_);
source = t->conds[data->index];
source.tobuffer(nullbuff);
If I'm not wrong, from the prototype of std::swap:
template <class T> void swap (T& a, T& b);
when you will call
Conditions* source;
Effect* source_;
std::swap(source, source_);
You will pass pointers by reference, which is fine. The prototype simply says that whatever you give to the function, it is passed by reference. So you can give it pointers, they will be passed by reference.
This is my understanding but I'm not sure.
However, since your pointers don't point to anything at this level, I'm not sure why you want to do that.
I tried it before and it cannot work. It is type mismash. pointer * is not the same type as &.
It results in error
trigedit.cpp(894): error C2784: 'void std::swap(_Ty (&)[_Size],_Ty (&)[_Size])' : could not deduce template argument for '_Ty (&)[_Size]' from 'Condition *'
visual studio 10.0\vc\include\algorithm(1029) : see declaration of 'std::swap'
trigedit.cpp(894): error C2784: 'void std::swap(std::_Vb_reference<_Alloc>,std::_Vb_reference<_Alloc>)' : could not deduce template argument for 'std::_Vb_reference<_Alloc>' from 'Condition *'
visual studio 10.0\vc\include\vector(1659) : see declaration of 'std::swap'
Also to explain why... I have two types (instances of two classes) which have similar data, but they are used for different purposes. There is a method which accepts data related to Conditions or related to Effects. And I want to create pointer "source" which will pointer type Condition or to pointer type Effects. The pointer should point to buffer keeping this data.
Hi again, I'm quite sure about what I said (but I can be wrong) and I made a simple test:
1 2 3 4 5 6 7 8 9 10
#include <iostream>
int main(){
int *p1;
int *p2;
std::swap(p1, p2);
return 0;
}
That compiles an run perfectly.
Now I notice that you're swapping two different things. If you use pointers, you have on one side a pointer to Conditions and on the other side, a pointer to Effect. You cannot swap two different types, and this is seen in the prototype of std::swap
that requires two arguments of the same type, passed by reference.
If Condition and Effect are two independent classes, then you cannot do what you want since it is not allowed (because it doesn't make sens).
If you say that they have some common stuff, then you can try to create a mother class from which Condition and Effect would be daughter classes.
Then you would be able to do something like this:
MotherClass *source;
if(data->type == Effect) {
source = new Effect();
}
else {
source = new Condition();
}
But this requires to stop for couple hours, and think about the design of these classes, see if it is really necessary, etc...
But I think I'm not understanding exactly what you want to do. The above code is what we do when we don't know a priori the exact class will be used: Condition or Effect.
Well, I will go back to mother class and have the methods to be declared in mother class. The thing I wanted to do originally is to avoid virtual methods. And now I realized (I hope that I am not wrong) that I don't need virtual methods because the methods which are under Condition and under Effect have same name, but they are defined in different classed so they don't need to be virtual.
A function calls void ECBase::tobuffer(Buffer &) const. Compiler has compiled the function, so the function has been declared in the scope. However, no object file (including the libraries) that is included in the linking stage does contain the implementation for the function.
Simpler example:
1 2 3 4 5 6 7
void foo();
int main()
{
foo();
return 0;
}
That file will compile, but the resulting object file cannot be linked to become an executable. Not without an object file that provides this:
Well, there is line in mother class ECBase: virtualvoid tobuffer(Buffer &b) const = 0;
with this lines it works fine. This does not print any error! But I thought that the abstract class is not needed. So I changed it to void tobuffer(Buffer &b) const;
Can you explain why it was not error when using abstract/virtual class?
I do not understand to the pure virtual function. * - OK, I've found answer: "A pure virtual function or pure virtual method is a virtual function that is required to be implemented by a derived class that is not abstract" - Wikipedia. So virtual function in general - needs not to be derived? For example when creating class template...?
A virtual keyword as I understand it - when you need to use some member in declaration or definition of some code like template but the value is unknown at the moment or it was not initiated yet. So it can be method or variable in a class. Using virtual means that the decision which method will be called is made during call of program, not in compilation. Also, now I read back: difference between earlier build and later build is at the normal function, there is a check if the method is really called on proper object. It could be the key problem. So when I use virtual keyword so it does not check something, but when I remove it so it checks and it sees some incompatibility.
To Aswer your question. I have seen using new operator to create instances from abstract class, however I am not aware if copy constructor is forbidden. I think it should not be problem to use it.