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
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.
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.
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.
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.
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.
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)
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.
#include <iostream>
usingnamespace 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.