Cannot convert to to pointer in initialization

closed account (DEUX92yv)
I've defined a class with a copy constructor. Some sample code:
Class* c = new Class(*this) // My copy constructor must take in a const Class&

And the compiler gives:
error: cannot convert 'Class' to 'Class*' in initialization


Why does this syntax work with other data types?
For example, int* MyInt = new int; compiles fine.
What about a copy constructor makes it fail?
Last edited on
Please show your copy constructor.
you should not use 'this' out of non-member function.
closed account (DEUX92yv)
Class has two data members: int* array, and int num, the number of elements in said array.

1
2
3
4
5
6
7
8
9
// Copy constructor
Class::Class(const Class& a)
{
    num = a.num;   // Number of elements in "array"
    array = new int[num];
    for (int i = 0; i < num; i++) {
        array[i] = a.array[i];
    }
}


The reason I used *this above is because I want to create a copy of the current Class object in this new object. Is there a better way to do this?
I do not see any wrong in the statement

Class* c = new Class(*this) // My copy constructor must take in a const Class&

except that there is no a semicolon in the end of it.

Topic archived. No new replies allowed.