3

I am using a library with a class that does not have an assignment operator implemented and the copy constructor is disabled. I can instantiate a local instance of LibraryClass named var like this:

LibraryClass var(data, (char *)fileName, results);

But I want to create a LibraryClass instance variable on the class I am writing. Then I want to instantiate it in the class constructor. Something like this:

class MyClass
{
    LibraryClass var;
    void MyClass();
}

MyClass::MyClass()
{
    var = LibraryClass(data, (char *)fileName, results);
}

In this case I end up with

error: ‘LibraryClass& LibraryClass::operator=(const LibraryClass&)’ is private
LibraryClass& operator=(const LibraryClass& rOther);  // no implementation

I have tried everything I can imagine to make this work but nothing is working. Is what I am attempting to do even possible? I am out of ideas so any suggestion is much appreciated.

EDIT

I'm not actually instantiating the variable in the constructor. It's happening in a separate function. I only said constructor because I mistakenly thought it was just a simplifying assumption. I didn't realize that the initialization list would solve that problem. The main question I want to answer is the title.

How can I instantiate an instance variable of a class that doesn't have a copy constructor or assignment operator? Or is the initialization list the only way to do it?

Mike S
  • 11,329
  • 6
  • 41
  • 76
  • 2
    So many duplicates and I can't find a single one of them... – juanchopanza Nov 20 '15 at 18:21
  • I couldn't find any duplicates. Perhaps you know some terminology I am not aware of that would lead to a duplicate? – Mike S Nov 20 '15 at 18:22
  • 1
    @juanchopanza what about: http://stackoverflow.com/questions/926752/why-should-i-prefer-to-use-member-initialization-list – NathanOliver Nov 20 '15 at 18:25
  • @LogicStuff If I had known that a "benefit of initialization lists" was that I could create an instance variable of a class that didn't have a copy constructor or assignment operator, then why would I have posted this question.... ? – Mike S Nov 20 '15 at 18:26
  • I don't know, but maybe you should grab a book that mentions member initializer lists immediately after mentioning user-defined constructors. – LogicStuff Nov 20 '15 at 18:30
  • 1
    That's the attitude that makes SO such a wonderful community. "Don't ask your perfectly useful novice questions. Instead, get a book and comb through every sentence searching for answers. SO is for expert questions only." – Mike S Nov 20 '15 at 18:35

1 Answers1

7

You need to use the initialization list.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
James Adkison
  • 9,412
  • 2
  • 29
  • 43
  • Is it feasible to do this without using initializer lists or is that the only option? – Mike S Nov 20 '15 at 18:56
  • 1
    @Mike The only other way I know of is to use a dynamically allocated object (e.g., have a `LibraryClass* var;` data member then perform `var(new LibraryClass(...))` in the initialization list or `var = new LibraryClass(...);` in the body of the constructor. **However**, I wouldn't use dynamic allocation unless you need to and at that point you should use a smart pointer (e.g., `std::unique_ptr`). **Note:** you should prefer initialization lists over performing assignment in the body of the constructor. – James Adkison Nov 20 '15 at 19:05