Skip to main content
gamedev.net
🔒 Locked

cpy ctr issue

Started by The C modest god Aug 26, 2006 at 2:08 PM 6 replies 1.8k views
Original Post
The C modest god
The C modest god
I have a class with both cpy ctr and operator=. There shouldnt be any problem doing the following?


template <class T>
class A {
   public:
      template <M>
      A (const A<M> & _Arg) { *this = _Arg; }; // <= Using operator= here wont cause problem?
      template <M>
      A & operator= (const A<M> & _Arg);
}
Thanks in advance.
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower. Quote: Original post by Toolmaker Quote:
dave
dave
Why are you templating with M for the methods and T for the class?

Dave
SiCrane
SiCrane
Problem 1: When you define a templated "copy constructor" and assignment operator, you still need to define the non templated versions for your class, or the compiler will call the default generated ones anyways.

Problem 2: No, don't do that. If you want to you can define assignment in terms of the copy constructor. ex:
A & operator=(const A & other) {  A temp(other);  this->swap(temp); // assuming you have a swap member function}

But doing the opposite can really hose you if the assignment operator assumes a class invariant that isn't true if the member variables are default constructed (which is the state your object will be in just before the assignment is called.

Problem 3: Don't name your variables with an underscore followed by a capital letter. Those identifiers are reserved for the implementation.
The C modest god
The C modest god
Quote:
Original post by SiCrane
Problem 1: When you define a templated "copy constructor" and assignment operator, you still need to define the non templated versions for your class, or the compiler will call the default generated ones anyways.

Problem 2: No, don't do that. If you want to you can define assignment in terms of the copy constructor. ex:
A & operator=(const A & other) {  A temp(other);  this->swap(temp); // assuming you have a swap member function}

But doing the opposite can really hose you if the assignment operator assumes a class invariant that isn't true if the member variables are default constructed (which is the state your object will be in just before the assignment is called.

Problem 3: Don't name your variables with an underscore followed by a capital letter. Those identifiers are reserved for the implementation.


Instead of using cpyctr in operator=, will it be good to create a private method that will be used in both, such as:
void copy (const A & other)A & operator (const A & other){  this->copy (other);  return *this}


Also, should I use the templated cpyctr (with M) inside the non templated cpyctr?
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower. Quote: Original post by Toolmaker Quote:
The C modest god
The C modest god
I have another problem.
(I will change _ soon)

I have a method defined, but I get compilation error about its implementation:

template <class T>class DefensePointer {   private:     template <class M>     void Copy (const DenfesePointer<M> & _Source);     DWORD * pInstancesAmount;}template <class T>template <class M>voidDefensePointer<T>::Copy (const DefensePointer<M> & _Source){   this->pInstancesAmount = _Source.pInstancesAmount; // <= Compilation error}


I get the following compilation error:
error C2248: 'DefensePointer::pInstancesAmount': cannot access private member declared in class 'DefensePointer' with [ T=Sprite ] and [ T=Sprite ]

Why do I get this error? can't I access private members of this?
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower. Quote: Original post by Toolmaker Quote:
Polymorphic OOP
Polymorphic OOP
Quote:
Original post by The C modest god
Why do I get this error? can't I access private members of this?

You are trying to access private members of the template instantiated with different template arguments (T vs M). When T and M happen to be the same type, you will not get this error, however, if T and M are different, you have to make sure that the instantiations are friends of each other in order to access each others members. (Edit: It's odd that you are getting that exact error as it seems as though it states that M and T are both "Sprite", so that error shouldn't be happening, unless I'm misreading it)

Inside the class template's definition, put:

template< typename M >friend class DefensePointer;


[Edited by - Polymorphic OOP on August 26, 2006 3:26:08 PM]
The C modest god
The C modest god
Quote:
Original post by Polymorphic OOP
Quote:
Original post by The C modest god
Why do I get this error? can't I access private members of this?

You are trying to access private members of the template instantiated with different template arguments (T vs M). When T and M happen to be the same type, you will not get this error, however, if T and M are different, you have to make sure that the instantiations are friends of each other in order to access each others members. (Edit: It's odd that you are getting that exact error as it seems as though it states that M and T are both "Sprite", so that error shouldn't be happening, unless I'm misreading it)

Inside the class template's definition, put:

template< typename M >friend class DefensePointer< M >;

I didnt understand where exactly I should put this?


It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower. Quote: Original post by Toolmaker Quote:
Polymorphic OOP
Polymorphic OOP
Quote:
Original post by The C modest god
I didnt understand where exactly I should put this?


template <class T>class DefensePointer {  template <class M>  friend class DefensePointer;/* ... */};

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.