Constructor with arguments of class

Hi you guys.
i had a class, in there, the constructor are using some input arguments, however in main program i can not declare the pointer of above class, mabe i had wrong implements.

could you help me solve this, or give another ideal to solve this problems.
here my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>

using namespace std;

class MyClass
{
public:
	MyClass(int ndim = 1);
	~MyClass();

private:
	double* x;
	double* y;
	double* z;
	double* t;
	double* h;
};

MyClass::MyClass(int ndim)
{
	this->x = new double[1];
	if (ndim == 2){
		this->y = new double[1];
	}
	else if (ndim == 3){
		this->y = new double[1];
		this->z = new double[1];
	}
	else if (ndim == 4){
		this->y = new double[1];
		this->z = new double[1];
		this->t = new double[1];
	}
	else if (ndim == 5){
		this->x = new double[1];
		this->y = new double[1];
		this->z = new double[1];
		this->t = new double[1];
	}
}

MyClass::~MyClass()
{
delete[] this->x;
delete[] this->y;
delete[] this->z;
delete[] this->t;
}

int main(){
	/*
	is ok if i declase a scalar variables
	*/
	MyClass a;

	/*
	however i cannot declare pointer with class "MyClass"
	with different argument of constructor.
	Fore example: pointer b with ndim = 2
	or pointer c with ndim = 4, ...
	*/
	MyClass* b(2); /*i got error in here*/
	MyClass* c(4); /*i got error in here*/
}
Last edited on
MyClass* b(2); /*i got error in here*/ MyClass* is an object of type pointer-to-MyClass, so you're trying to pass the variables to the constructor of a pointer-to-MyClass.

First make the object of type MyClass, and then make a pointer that points to it.

MyClass* b = new MyClass(2);
Don't forget to delete it when you're done with it.

Alternatively,
1
2
MyClass a(2);
MyClass* b = &a;
Last edited on
whoh, thank Moschops!

so, Is that an only way to use declare pointer of type MyClass?
MyClass* is the way to declare a pointer of type pointer-to-MyClass.

A pointer-to-MyClass is a completely different object to an object of type MyClass. Creating a pointer-to-MyClass does not create an object of type MyClass.
Lines 12-16: It's not at all clear why you're using pointers here. Why is it necessary to dynamically allocate these variables?

You have a memory leak. Lines 21-28 allocate memory, but you never release that memory. That is a memory leak. Your destructor should release the dynamically allocated memory.

Line 36: You're allocating 2 doubles. Is that a typo?

Is that an only way to use declare pointer of type MyClass?
Moschops gave you the two primary ways of declaring a pointer to an instance of your class.
Last edited on
in line 36 i had wrong typing, that is this-y = new double[1];
however the Moschops's method just used poiter point to scalar, actually i want use pointer point to array, or an array of class MyClass or something like this one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>

using namespace std;

class MyClass
{
public:
	MyClass(int ndim = 1);
	~MyClass();


	double* x;
	double* y;
	double* z;
	double* t;
	double* h;
};

MyClass::MyClass(int ndim)
{
	this->x = new double[1];
	if (ndim == 2){
		this->y = new double[1];
	}
	else if (ndim == 3){
		this->y = new double[1];
		this->z = new double[1];
	}
	else if (ndim = 4){
		this->y = new double[1];
		this->z = new double[1];
		this->t = new double[1];
	}
	else if (ndim == 5){
		this->x = new double[1];
		this->y = new double[1];
		this->z = new double[1];
		this->t = new double[1];
	}
}

MyClass::~MyClass()
{
delete[] this->x;
delete[] this->y;
delete[] this->z;
delete[] this->t;
}

int main(){
	/*
	is ok if i declase a scalar variables
	*/
	MyClass a;

	MyClass b(1);
	MyClass* c = &b;
	for (MyClass* temp = c; temp < c + 100; temp++){
		*temp->x = 1.;
	}
	MyClass* d = new MyClass(4);
	for (MyClass* temp = d; temp < d + 1000; temp++){

	}

}
Last edited on
That's not going to work.

Line 52 allocates one instance of your class. c points to that one instance.

Line 54: Second iteration of your loop is going to cause an out of bounds reference. There is no instance of your class at c[1] i.e. *(temp + 1).

Line 58: Same problem.

If you want 100 instances of your class, you can do this:
 
  MyClass * e = new MyClass[100];

Now your loop at 54 is legal.

BTW, if you don't fix the problems pointed out to you, you will find people less willing to help you.
- You haven't explained why lines 12-16 are pointers.
- You haven't fixed the memory leak.
- You haven't fixed line 36.
Last edited on
@ AbstractionAnon: if i use MyClass * e = new MyClass[100];
i can not mofidier ndim parameter :)).
Do you have to use your own class? Why not just use a vector of doubles?
Hi,

Why are you using new, delete, raw pointers and arrays? Consider using std::vector, and all of your problems will probably go away. The STL was designed to be easy and safe to use. You could just have std::vector<MyClass> MyObj then use MyObj.push_back to put stuff into it, don't worry about how many items you may have - the vector can automatically grow.

Note that STL containers store their data on the heap, and do their own memory management. Doing your own memory management is error prone.

new and delete aren't really necessary in modern C++, and they cause problems.

Good Luck !!
> or give another idea to solve this problems
if we only know what problem you are trying to solve...
http://www.cplusplus.com/forum/general/138037/


By the way, you are missing {copy,move}-constructor and assignment operator. And you fail to initialize some variables, for example, if ndims=3, you don't initialize `t' nor `h'. That'll cause undefined behaviour when you try to delete them in the destructor.
(also, got a leak on line 35 as you overwrite `x', and please don't edit the OP, it makes harder to follow the responses)


> Why not just use a vector of doubles?
I concur.
Why not just use a vector of doubles?


The OP has 5 double variables, they look like point data. Would it be better to have a struct for them, then make a PointCollection class which has a vector of the struct as a private variable? Alternatively, make Point a class with an interface, then make a std::vector<Point>
if i don't use pointer in side the class, the program working well, however, in some cases i just use x, y, z (ndim = 3) or x, y, z, t (ndim = 4), so in that case t, or h are not used, then waste memory (because my program will need some array with milions component).
i tried to use vetor, so i think working with pointer is faster than working with vector (working with vector more comfortable than working with pointer but no problem).
at the end, i still can not declare an array with an parameter inputing to constructor.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>

using namespace std;

class MyClass
{
public:
	MyClass(int ndim = 1);
	~MyClass();
	double* getX_comp();


	double x = 0;
	double y = 0;
	double z = 0;
	double t = 0;
	double h = 0;
};

MyClass::MyClass(int ndim)
{
}

MyClass::~MyClass()
{
}

double* MyClass::getX_comp(){
	return &this->x;
}

int main(){
	/*
	is ok if i declase a scalar variables
	*/
	MyClass a;
	/*
	however i cannot declare pointer with class "MyClass"
	with different argument of constructor.
	Fore example: pointer b with ndim = 2
	or pointer c with ndim = 4, ...
	*/
	MyClass b(1);
	MyClass* c = &b;
	for (MyClass* temp = c; temp < c + 100; temp++){
		cout << *temp->getX_comp() << "\n";
		temp->x = 1.;
	}
	MyClass* d = new MyClass(4);
	for (MyClass* temp = d; temp < d + 1000; temp++){

	}

}
so in that case t, or h are not used, then waste memory

That's nonsense. You're actually taking more space using pointers, regardless of how many dimensions you specify. Pointers take memory whether or not you allocate something for them to point to.

Besides that, every new operation on the heap has overhead for boundary tags before and after each element allocated.

On a 64 bit system, 5 doubles = 40 bytes regardless of the number of dimensions.

5 pointers also take 40 bytes. Then add to that probably 24 bytes (2 boundary tags + double) for each double you allocate via new on the heap. So that's a minimum of 64 bytes, assuming 1 dimension and goes up to 168 bytes with 5 dimensions.


Last edited on
@TheIdeasMan
1
2
3
4
class MyClass{
private:
   std::vector<double> x; //x = x[0], y = x[1], ...
};
However, vector has overhead, it may be better to just use an array.
And if the number of elements of `MyClass' would not change on runtime, then
1
2
3
4
5
template<int N>
class MyClass{
private:
   std::array<double, N> x;
};
Topic archived. No new replies allowed.