You shouldn't be using raw pointers at all here. There is no reason for it. It's just an invitation to make tons of errors.
Secondly, the syntax you have for initializers is very broken, that's not how the syntax works at all. I'll put in an example of what initializers should look like in here so you can see. You also misunderstand what new
does. You don't need any of the assignments that you're doing. Not one of them.
Also, you've misspelled public
as publuc
. And the way you declare your constructor is not correct for declaring it as a class member.
You know, almost all the problems you have, the compiler should've given you at least vaguely sensible error messages about, and you shouldn't need us to fix them for you.
Here is an example class that uses some pointer members. Note that if you are using pointer members, especially as a C++ beginner, you are almost certainly doing something wrong. Bare pointers should be among the last things you learn about in C++, not the first:
class IHaveSomePointers {
public:
IHaveSomePointers(bool const &mybool, int const &myint)
: mybool_{new bool(mybool)}, myint_{new int(myint)}
{ }
~IHaveSomePointers() {
delete mybool_;
delete myint_;
}
IHaveSomePointers(IHaveSomePointers const &) = delete;
void operator =(IHaveSomePointers const &) = delete;
private:
bool *mybool_;
int *myint_;
};
This class does have one problem. If allocation of myint_
throws an exception, there will be a memory leak. This kind of thing is why you do not use raw pointers in C++, especially for member variables, and most especially if the thing they're going to be pointing at is allocated with new
.
I deleted the copy constructor and assignment operator because they need special implementations for classes that contain raw pointers. I notice that you appear to have been trying to define your own copy constructor, but it's hard to tell because your constructor declaration is so very garbled.
Here is how this class should be written:
class IDontHavePointersBecauseThatsBad {
public:
IDontHavePointersBecauseThatsBad(bool const &mybool, int const &myint)
: mybool_{mybool}, myint_{myint}
{ }
private:
bool mybool_;
int myint_;
};
If you absolutely must dynamically allocate things, do this:
#include <memory>
class ForSomeBizarreReasonIDyanmicallyAllocate {
public:
ForSomeBizarreReasonIDynamicallyAllocate(bool const &mybool, int const &myint)
: mybool_{::std::make_unique<bool>(mybool)},
myint_{::std::make_unique<int>(myint)}
{ }
private:
::std::unique_ptr<bool> mybool_;
::std::unique_ptr<int> myint_;
};
That class doesn't need a destructor to make sure it deletes the memory it allocates. It doesn't have a potential memory leak. It's all around a better class.