-2
class shape{
public:
    shape(string a, bool b):name(new string), moral(new bool){
        *name=a;
        *moral=b;
    }
    shape():name(new string),moral(new bool){
        *name="shape";
        *moral=true;
    }
    ~shape(){
        delete name;
        delete moral;
    }

protected:
    string* name;
    bool* moral;
};


class circle:public shape{
public:
    circle(string s, bool bb, double d):shape(new string, new 
bool),radius(new double){


    }
protected:
    double * radius;
};

Recently, I was trying to pick up c++. Here is a sample code I wrote when learning the property of inheritance. There are errors show on "shape(new string, new bool)" in child class circle. I am not sure what is right syntax to do that. Also, I noticed if were using pointers in classes, the form of initialize list were used to allocate memory instead of assigning values. Are there better expressions and syntax I can use to do both? Thank you guys in advance.

  • 3
    Why are you using `name:(new string)` instead of just `name:(a)`? – Barmar Sep 15 '17 at 23:03
  • name is a pointer I created. For dynamic memory, shouldn't I allocate memory first? – Tian Yuan Sep 15 '17 at 23:07
  • Why do you need it to be dynamic? Why not just have a `string` as a member instead of `string*`? – Barmar Sep 15 '17 at 23:11
  • Dynamically allocating a `string` or a C++ container like `vector` or `list` defeats a large part of their purpose: They are designed to handle the memory management for you. – user4581301 Sep 15 '17 at 23:11
  • It is also true for int double and other containers? – Tian Yuan Sep 15 '17 at 23:21
  • C++ is not Java. Why would you want to allocate a bool on the heap? –  Sep 15 '17 at 23:30
  • 1
    You cannot learn C++ by just experimenting, and definitely not by doing what you have done in Java. Your code is not just slightly in error, it is **way** off. Please try reading [a book on C++](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Please! – Bo Persson Sep 15 '17 at 23:33
  • I do not know why I want to do this. I was just learning the language step by step, and eventually wandering up "how about allocating some containers on heap in class to practice inheritance in that situation" – Tian Yuan Sep 15 '17 at 23:39
  • The problems have nothing to do with inheritance but with how to allocate memory for variables. I guess you use lots of pointers so the compiler doesn't complain about the use of `new` everywhere. But unlike Java, in C++ you *hardly ever* have to use `new`. – Bo Persson Sep 16 '17 at 00:38

1 Answers1

1

prefer to deal in values. c++ is not like java or c#. Avoid pointers, new and delete for this kind of thing.

If you don't write a destructor, the compiler gives you correct destructors. copies and moves for free.

#include <string>

class shape{
public:
    shape(std::string a, bool b) : name(a), moral(b)
    {
    }
    shape():name("shape"),moral(true){
    }

protected:
    std::string name;
    bool moral;
};


class circle:public shape{
public:
    circle(std::string s, bool bb, double d)
    : shape(s, bb)
    , radius(d)
    {

    }
protected:
    double radius;
};

But in the real class I want to use dynamic memory for storage:

#include <string>
#include <memory>

class shape{
public:
    shape(std::string a, bool b) 
    : name(std::make_unique<std::string>(a))
    , moral(b)
    {
    }

    shape() 
    : shape("shape", true)
    {
    }

protected:
    std::unique_ptr<std::string> name;
    bool moral;
};


class circle:public shape{
public:
    circle(std::string s, bool bb, double d)
    : shape(s, bb)
    , radius(d)
    {

    }
protected:
    double radius;
};
Richard Hodges
  • 68,278
  • 7
  • 90
  • 142
  • Well, I know how to handle values but pointers, that is the reason why I am asking if there are correct and simple syntax to use dynamic memory in class, especially when handling inheritance. – Tian Yuan Sep 15 '17 at 23:12
  • @TianYuan ok will update to demonstrate dynamic memory with smart pointers – Richard Hodges Sep 15 '17 at 23:13