The right way of C++ class init() function
Hi all,
Could you teach me the right way of C++ class initialization function, init() ? Code:
// This class is intended to be reused many times. |
you don not use a init function, you implement the init code direct in the costructor
better, you initialize your members direct. do not use int8_t *buf and len as dynamic arrays, use std::vector<int8_t>, it will make you more happy |
Cannot use vector<> in my example. I need the pointer to a C structure.
OK. reset() was what I had intended when I first drafted this post. Code:
// This class is intended to be reused many times. In this case how should reset()s and the ctor()s should be arranged? |
you should insert trace messages into your code to see what's happening, or probably you may try a debugger to test it (ddd).
Trace means something like that: Code:
printf("trace: %s class %s method %s", classname, methodname, checkpoint) In general you do not need to call reset in HelloMessage, because it will be automatically invoked from the inherited constructor, but you will see that using those trace. |
Thank you for hinting. I think I found the right way:
Code:
class Message // name reduced for easy recognition |
you must not implement a virtual function - or at least I think so -, therefore reset of class Message is wrong. But probably I missed something...
Would be nice to see how did you compile it. You ought to use -Wall |
Quote:
https://en.wikipedia.org/wiki/Virtua...tual_functions Quote:
Quote:
|
@Pan64 I don't get your reply or why. Please give more details.
|
Many web search results say that virtual-ness is propagated to the derived class. Even cplusplus.com has an example on Polymorphism that the derived class does not declare the virtual function with the 'virtual' keyword in the derived classes.
Anyway, my personal choice would be sticking with the 'virtual' keyword so that when writing the next derived class I would immediately know which function is virtual. Thank you all. It was a good discussion and knowledge mining session. |
Methods such as init() and reset() (the names are not significant ...) are sometimes used to avoid timing issues, or simply by convention within a particular programming team. Constructors run in a somewhat-peculiar environment in which, sometimes, "things aren't quite set up yet." So, objects sometimes carry their own initialization and clean-up functions that are intended to be called separately, after the constructor has run and before the destructor. They can also be used to "recycle" an object for re-use, resetting it to its initial state. Programmers are obliged to make explicit calls to them. (The case that you illustrate, where the constructor calls the function, is not one that I commonly see.)
|
Quote:
It results in code that's not exception-safe. If you have code like that, you can't use exception-handling. If you do, you'll get resource leaks. It's common because a lot of C++ code (Qt, for example) was designed back when exceptions weren't ready for production use. |
Quote:
|
All times are GMT -5. The time now is 11:03 PM. |