2

I'm a beginner to c++ so there are a lot of things quite not clear in my mind.

I have this code I need to write and in a class I make a constructor. However, I don't need any parameters because I read from a file-stream inside the constructor. So my questions are:

1.Can I make a constructor like this:

class myClass {
private:
  string title;
  string organizer;
public:
  myClass() {
    title = stringRead();
    organizer = stringRead();
  }
}

where stringRead() is a function I have written to read from my file??

2.How do I call it afterwards when I need it? I know that the default constructror is being called like that:

myClass A;
A = myClass();

Is it the same?

3.If I have a pointer, how do I call the constructor again? This doesn't seem like it should be right...

myClass *B;
B = myClass();

Thanks in advance! =D

1

4 Answers 4

3

1) This constructor will work but you should favor using an initialization list (assuming stringRead() isn't a member function of myClass

class myClass {
private:
  string title;
  string organizer;
public:
  myClass() 
    : title(stringRead()),
      organizer(stringRead())
  { }
};

2) myClass A; is what you should be doing. You could alternatively have auto A = myClass(); which, after optimizations, will be the same thing. Without optimizations a temporary will be constructed, and then A will be move constructed from it, so this won't work with unmovable objects (your object is movable)

3) If you want to use a raw pointer then you would use

myClass *ptr = new myClass;
// bunch of code
delete ptr;

However, you'd be better using a smart pointer to control its lifetime. This way you won't need to manually delete

std::unique_ptr<myClass> ptr(new myClass);

or make_unique in c++14

auto ptr = std::make_unique<myClass>();

And of course use a shared_ptr if you have shared ownership

Sign up to request clarification or add additional context in comments.

4 Comments

about 2: I have a class in a class so there I have to write class myClass1: private: myClass2 element; public: myClass1() { /*call constructor of myClass2*/ }
check it again. I hit 'enter' by mistake before finishing writing. =]
@Anina7 that doesn't parse. Are you trying to inherit or you have a myClass2 data member? In either case you'd still use the initialization list syntax for your constructor but the name will vary.
probably: myClass1() : myClass2(/* ctor args */) { }
2

I think it's OK to assign the value returned by a function to a member of a class.

You can initialize it as you suggested (with myClass A;)

When you use pointers, you need myClass *k=new myClass();. You should remember to delete the object you created with delete k;.

Comments

2
  1. Your constructor is fine, so long as the functions used within it are globals or static functions of this or another class.

  2. myClass A; will invoke the constructor you have written.

  3. To use a pointer, you need B = new myClass(). That will also call the same constructor. Don't forget to delete B at some point else you'll leak memory.

Do bear in mind that if an exception is thrown in a constructor then the destructor is not called.

Comments

2
  1. Yes, you can, but it might not be the best approach. Reading from input can fail, failure in a constructor is often a non-recoverable event you'll want to handle. A good approach is reading the values outside the costructor, handling errors and calling the constructor only when you have "everything ready". Like this:

    class myClass {
    private:
    string _title;
    string _organizer;
    public:
      myClass(const string &title, const string &organizer) {
        _title = title;
        _organizer = organizer;
    }
    

    or, by using a more idiomatic C++ initializer list:

    class myClass {
    private:
    string _title;
    string _organizer;
    public:
      myClass(const string &title, const string &organizer):
        _title(title), _organizer(organizer) {}
    }
    

    and then, somewhere else:

    string title = stringRead();
    string organizer = stringRead();
    myClass A(title, organizer);
    
  2. No, in this snippet:

    myClass A;
    A = myClass();
    

    two different things happen: at line 1 the default constructor is called; at line 2, a temporary object is constructed (again, by calling the default constructor) and then assigned to A using the (rval for C++11) copy operator. This expression:

    myClass A;
    

    calls the default constructor. If you have parameters:

    myClass A(title, organizer);
    
  3. Nope, this does not even work. A pointer is not an object, you have to allocate the object. At that point, you can get a pointer to it:

    myClass A;
    myClass *B = &A;
    

    you could also resort to dynamic allocation:

    myClass *B = new myClass;
    

    in this case, either remember to call delete B somewhere else or wrap B in a smart pointer:

    std::unique_ptr<myClass> B(new myClass());
    

2 Comments

your constructor has added leading underscores on the data members, and it is a java style constructor. You should use an initialization list
@RyanHaining the initializer list is certainly more idiomatic, but it might not be clear to a true beginner like OP is. Anyway, I added both.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.