0

Hey guys I trying to make abstract base class and I made this. It working without any issues, but im not sure if I can use operator new like this in declaration. I thought that I can use operator new just in constructor but it looks like that I can use operator new like this:

#include <iostream>
using std::cout;

class abs
{
private:
    int* x = new int;
    int* y = new int;
protected:
    static int count;
public:
    virtual void show() = 0;
    abs() { *x = 0; *y = 1; count++; cout << "Vytvoren objekt tridy ABS: " << count << "\n"; };
    virtual ~abs() { delete x; delete y; count--; cout << "Objekt tridy ABS zrusen: " << count << "\n"; };
};

class one : public abs
{
private:
    int* z = new int;
public:
    virtual void show();
    one();
    virtual ~one() { delete z; count--; cout << "Zrusen objekt tridy ONE: " << count << "\n"; };
};

#endif

Is it possible to use it like this or is it wrong?

MikeCAT
  • 61,086
  • 10
  • 41
  • 58
  • 6
    You can, but you shouldn't. Here is the full list of cases where you should use `new`: \[]. (Yes it is empty). – n. 'pronouns' m. Apr 22 '21 at 10:25
  • there are two somewhat orthogonal issues. One is in-class initializers: https://stackoverflow.com/questions/27352021/c11-member-initializer-list-vs-in-class-initializer. The other issue is: Why are you using (raw) pointers and `new` at all? – 463035818_is_not_a_number Apr 22 '21 at 10:25
  • 3
    You fail to comply to the [rule of three](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three), but otherwise it would work. The question is why do you want pointers in the first place? – Yksisarvinen Apr 22 '21 at 10:27
  • 1
    Creating a pointer to a single `int` is actually kind of absurd. There's absolutely no reason for pointers here. It only makes a mess of things. – tadman Apr 22 '21 at 10:34
  • I learning c++ for 3 months so im still new user, but the reason in this example why I use pointer is because I tryiing it and i getting acquainted with it. So any advice means gold for me. – Haumikj Haumer Apr 22 '21 at 10:36
  • rather practice smart pointers, if you make your members `std::unique_ptr` then it is still a little absurd to use dynamically allocated single `int`s, but your code will be much less broken. THough to appreciate smart pointers probably you need to understand the issues with raw owning pointers. Try to make some copies of objects of your classes to see the trouble – 463035818_is_not_a_number Apr 22 '21 at 10:48
  • 1
    Note that lots of books (some new too) are repeating patterns which have seance in '90. Nowadays there is no reason to explicitly use operator `new` and `delete`. You should use `std::unique_ptr` and `std::shared_ptr` which are great tools to leverage RAII pattern. So if your book have a chapter about memory management and do not mentions `std::vector`, `std::unique_ptr` and `std::shared_ptr` then find another book. Based on code you've wrote you have such book. This might help https://stackoverflow.com/a/388282/1387438 – Marek R Apr 22 '21 at 10:50
  • It is kind of necessary to use operator `new` when implementing the standard library, for example when implementing the default/standard allocators. Admittedly, most developers *use* the standard library rather than creating an implementation of it. – Peter Apr 22 '21 at 11:37
  • I reading book C++ Primer Plus, 6th edition by Stephen Prata. Is it great book or what do you guys think? Its from year 2012. – Haumikj Haumer Apr 22 '21 at 12:16
  • I'd read Stroustrup, Stroustrup, then Stroustrup. And practise as you go. – Bathsheba Apr 22 '21 at 12:36

0 Answers0