Initialize the variable again

This may seem a bit confusing. Basically, I have a function

CCard newCard() { /* Used to store the string variables intermittantly */ std::stringstream ssPIN, ssBN; int picker1, picker2; int pin, bankNum; /* Choose 5 random variables, store them in stream */ for( int loop = 0; loop < 5; ++loop ) { picker1 = rand() % 8 + 1; picker2 = rand() % 8 + 1; ssPIN << picker1; ssBN << picker2; } /* Convert them */ ssPIN >> pin; ssBN >> bankNum; CCard card( pin, bankNum ); return card; } 

which creates a new CCard variable and returns it to the caller

CCard card = newCard();

My teacher advised me that this is a violation of OOP principles and should be placed in class. He told me to use this method as a constructor. What I've done:

 CCard::CCard() { m_Sperre = false; m_Guthaben = rand() % 1000; /* Work */ /* Convert them */ ssPIN >> m_Geheimzahl; ssBN >> m_Nummer; } 

All variables with m_ are member variables. However, the constructor works when I initialize the map normally

CCard card();

at the beginning of the program. However, I also have a function that should create a new map and return it to the user, this function is now broken.

Original command: card = newCard(); is no longer available, and card = new CCard(); does not work. What other options do I have? I feel that using the constructor will not work, and that I probably should just create a newCard class method, but I want to see how it is generally possible to do as the teacher wanted.

This creates a lot of headaches for me. I told the teacher that this is a stupid idea, and not everything should be classified in OOP. Since then, he told me that Java or C # does not allow code to be used outside of classes, which sounds a bit unbelievable. Not sure if you can do this in C ++, especially when there are boilerplate functions or general algorithms. Is it true that this would be bad code for OOP in C ++ if I hadn't injected it into a class?

EDIT:

I want to thank everyone for their helpful answers! However, I believe that my wording of the question is a little screwed up, and I think that people do not understand what I'm looking for.

I do not want to initialize another element of type CCard. I want to intitialize

CCard card once, and then give the card new values ​​through the constructor, because this is what the teacher told me to do. I do not want to create a new CCard object, just use the same variable with the new values ​​again.

This is why I said that this probably won't work with the constructor. Therefore, I have a function that should take the initialized variable card , and then call the constructor again ("What?" Is what I told the teacher), and then give it new values.

Code example:

 void foo() { /* Initialize card with constructor */ CCard card; /* Give it new values through the constructor AGAIN... */ card; } 

This is a topical issue. Sorry if I confused everyone xD

+4
source share
4 answers

Just some basic pointers, because you have some misunderstandings.

This does not initialize the CCard object. It declares card function that returns a CCard and does not accept any parameters.

 CCard card(); 

If you want to build a CCard object, just do it.

 CCard card; 

Your constructor will be called and card must be properly initialized.

The expression new CCard() dynamically creates a CCard object and "returns" a pointer to the CCard object, which you then need delete . I do not recommend using this until you have created and understood the use of local objects first.

EDIT in response to editing a question

A constructor can only be called once in the life of an object, so you can never "rebuild" an object. However, if your class is assigned, you can usually assign it a default value created by a temporary:

 card = CCard(); 
+7
source
 CCard card; 

is the way to use the default constructor.

But your teacher is mistaken, in C ++ we will use free functions to greatly expand the functionality.

In your case, it is wise to use a constructor, because it is implied in this way, unless you have special needs, you do not need to make newCard: this will create unnecessary copies.


The “new” is reserved for pointers, so you need to use something like this:

  CCard * card = new CCard(); 

And you need to explicitly specify delete them. But pointers should be used only when necessary and, if possible, not “raw”, but smart. (see smart pointers like boost :: shared_ptr)


FOR YOUR PICTURE

You can specify new CCard values ​​using:

-) CCard method as:

 card.newValues(); 

The constructor can also call "newValues ​​()" to avoid redundancy.

-) - a free function (or a member of another class) that accepts CCard as a reference parameter:

  void newValues( CCard & card ) { /* set new values */ }; newValues( card ); 

-) directly

  card.set( /*values*/ ); 

Maybe with a set of accessories (set / get), if that means something to you.

The question you need to ask yourself is whether you want to change the internal elements of the class with accessories or want to change its value inside the class.

-) If you really want to call the constructor again, you can make a copy:

  CCard c; c = CCard(); 

But it is like creating a new CCard.

+3
source

I'm also new to C ++, but I think you might need a copy constructor to return objects from functions:

 CCard::CCard (const CCard &rhs) : m_Gehiemzahl(rhs.m_Geheimzahl) // and so on for each member variable { } 

Remember to add a prototype to the class declaration.

+1
source

What your teacher is trying to do is that you do not need new objects. Instead, you need to provide a way to interact with / manipulate the objects you have. You do not need to restore the object, just change the internal state of the object.

Of course, there is a big step towards the "principles of a functional language", where a volatile state is considered a great source of errors. But the volatile state itself is bad; it is simply a fact that making changes to the internal state of an object is not always obvious. You want to make it obvious.

0
source

Source: https://habr.com/ru/post/1311481/


All Articles