Anina7
Anina7

Reputation: 93

Class constructors in C++

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

Upvotes: 2

Views: 318

Answers (4)

Stefano Sanfilippo
Stefano Sanfilippo

Reputation: 33096

  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());
    

Upvotes: 2

Ryan Haining
Ryan Haining

Reputation: 36882

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

Upvotes: 3

Bathsheba
Bathsheba

Reputation: 234865

  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.

Upvotes: 2

ForceBru
ForceBru

Reputation: 44888

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;.

Upvotes: 2

Related Questions