András
#cppbookguide
SjD
I think #cppbookguide will better in this case
there was a c++ link in the on the c page
SjD
so,thanks anyway
Augmented
klimi
A HA HA HA... totally haven't seen this 800x
Augmented
0xff
Md. Shahib
/get ide
Md. Shahib
/get best-book
Md. Shahib
/get best
Md. Shahib
/get cpp
Md. Shahib
/get cbook
Optimus
We can't create object for abstract class. Then what is the reason for using abstract class?
olli
Hami
Kitty memory?
Augmented
Augmented
Hi friends, is this singleton approach correct? If not, where are the possible flaws?
Francisco
Hi friends, is this singleton approach correct? If not, where are the possible flaws?
I'd say that you should avoid sigletons, but yes, is correct. Maybe you can't access the instance pointer because it's private, but everything else should be okay. I don't know if the compiler generates more constructors, but you should check for copy, move, and assignments
Francisco
Just to be 100% sure you can't instanciate it
Francisco
Also, you may want to initialize the pointer inside the class, like static Singleton* instance = nullptr;
Augmented
error: non-const static data member must be initialized out of line
Francisco
I was trying to do this in the header file
I've just checked and it seems that you can't initialize a non-integral memeber inside the class, which is quite surprising for me. Also, maybe you prefer deleting the constructor rather than making them private
Augmented
Okay, you can't, you need to initialize the class
Yes, is it good to use such basic singleton class implementation, as a parent for deriving different classes which must stay singleton?
Francisco
Yes, is it good to use such basic singleton class implementation, as a parent for deriving different classes which must stay singleton?
I personally wouldn't use singletons at all. But if you need it, I think you can do that
Augmented
Francisco
I guess you can, It's in the design patterns
Augmented
I personally wouldn't use singletons at all. But if you need it, I think you can do that
Thanks, I am writing a driver for CAN hardware, and software implementation of the CANOpen stack, for embeded system with no OS., There are limited number of peripherials I must handle, and I want to be sure, that it's memory leak free, and drivers can't be multiple times instantiated...
Augmented
It's not thread safe
How can be fixed? Anyway I am not using real-multitasking, but simple process forking with a lot of state machines... but I have hardware interrupts
olli
How can be fixed? Anyway I am not using real-multitasking, but simple process forking with a lot of state machines... but I have hardware interrupts
I'd just go with class Singleton { Singleton() = default; Singleton(const Singleton&) = delete; public: static Singleton& instance() { static Singleton instance_{}; return instance_; } }; assuming >= C++11
Augmented
Is this lambda like?
olli
Is this lambda like?
no, but C++11 gauarantees thread-safe initialization of static varaibles in functions
Augmented
no, but C++11 gauarantees thread-safe initialization of static varaibles in functions
But it's reference, not pointer... i didn't know it's allowed that way
Augmented
no, but C++11 gauarantees thread-safe initialization of static varaibles in functions
Can i use an initialiser list in the private constructor declatration, or i must intialise variables else where?
olli
Can i use an initialiser list in the private constructor declatration, or i must intialise variables else where?
not sure i fully got what you meant, but sure you can use initializer lists in private constructors
Augmented
not sure i fully got what you meant, but sure you can use initializer lists in private constructors
Okay, thank you very much for the briliant idea, now i am experimenting with it, and seems to do the job... I can't test it now within a multitasking environment, but will try it with heavy loaded interrupts... I assume there isn't any possibility for memory leaks?
Augmented
Khaleed
Hy
klimi
Hy
No, c++ or C group only
Khaleed
I know
Artöm
Its better to delete in public part
Augmented
Its better to delete in public part
Okay, and default stays in private?
Artöm
Yes
Augmented
Yes
Here I wouldn't need copy/move constuctors anyway, but it's good to know what's the proper way...
Artöm
They need to be deleted
Artöm
Singleton cannot be copied or assigned
Augmented
Singleton cannot be copied or assigned
yes, i moved to public these with = delete
Augmented
Artöm
Why virtual?
Artöm
You need protected ctor then
14•08
Anonymous
C++ book for beginner
Francisco
/report
Liam
/ban for ad.
Anonymous
int a=4,b=5; cout«(a>b)?(a-b):(b-a); Output : 0 Why not 1?
David🇨🇺ElChino
C++ book for beginner
Beginning C++17
David🇨🇺ElChino
Francisco
int a=4,b=5; cout«(a>b)?(a-b):(b-a); Output : 0 Why not 1?
Try std::cout << ((a>b)?(a-b):(b-a));
Francisco
Not quite sure what's going on without the extra parenthesis
Anonymous
Why don't you use the traditional if?
😑 That's not the point
David🇨🇺ElChino
Anonymous
why not?
Using if is always an option, I was asking what's wrong with this method
David🇨🇺ElChino
👍
MilkBeforeCereal
int a=4,b=5; cout«(a>b)?(a-b):(b-a); Output : 0 Why not 1?
Because ?: has lower precendence than <<
MilkBeforeCereal
so it just gets (a>5) as false or 0 and displays that
Francisco
^
I've just realized after checking the assembly code (the specialization in stream for bool is called)
David🇨🇺ElChino
It depend
I think the one I recomended is quite fine