-1

I am trying to implement an explicit conversion constructor for an assignment and I am confused what is it I am required to do. I have a WordList containing a single element, and am to make this constructor explicit so I cannot do:

WordList myList;
list = 'i'; // error
sp_m
  • 2,647
  • 8
  • 38
  • 62
Jon
  • 2,249
  • 6
  • 26
  • 30
  • What assignment operators do you have for WordList? – Akanksh Mar 20 '12 at 16:37
  • That's assignment (but with a typo in the names) not construction – Flexo Mar 20 '12 at 16:37
  • possible duplicate of [What does the explicit keyword in C++ mean?](http://stackoverflow.com/questions/121162/what-does-the-explicit-keyword-in-c-mean) –  Mar 20 '12 at 16:38
  • This question is too vague. What does "assigning a word to a world-_list_" mean? Then you have not shown your definition of WordList. Then, 'i' is not a string, but a char. My best advice: Before diving into advanced topics, first learn the basics. – Sebastian Mach Mar 20 '12 at 16:38
  • Have a member function: 'Wordlist& operator= ( cons char* c );' – Akanksh Mar 20 '12 at 16:38
  • 1
    @Akanksh: He's assigning chars and he wants to specifically _forbid_ that. I think the question is too vague anyways. – Sebastian Mach Mar 20 '12 at 16:40
  • The only assignment operator I have for WordList is '='. Sorry, I am not clear on my terminology as I am taking C++ for the first time. – Jon Mar 20 '12 at 16:42
  • @phresnel : Interesting, now that you mention it, I am not quite sure what he/she is asking. Is it assignment operator? disable assignment operator? Ctor? Explicit Cotr? – Akanksh Mar 20 '12 at 16:42
  • @user1277607: Imho, that is really not a clean design. What does it mean to initialize a list-of-words with a letter? To me, this smells like there is a [god-object](http://en.wikipedia.org/wiki/God_class) or [feature-creep](http://en.wikipedia.org/wiki/Feature_creep) growing up. – Sebastian Mach Mar 20 '12 at 16:57

1 Answers1

8

All that you need to use explicit keyword as:

class WordList 
{
   explicit WordList(char c) {}
};

WordList w = 'i';  //error
WordList v ('i') ; //ok
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • I tried placing the keyword explicit in front of my constructor in both my .cpp and .h file, but I received the error "illegal storage class". I can't post my code up here incase my prof sees it. – Jon Mar 20 '12 at 16:39
  • @user1277607: No. Do that only in .`h` file, in the declaration only. In my example, I defined it in the class itself, so `explicit` is allowed here in this case (in the definition). – Nawaz Mar 20 '12 at 16:40