Advanced C++: Static Initialization Fiasco

Поделиться
HTML-код
  • Опубликовано: 28 ноя 2024

Комментарии • 22

  • @BoQianTheProgrammer
    @BoQianTheProgrammer  11 лет назад +5

    Very good observation. I am actually using C++ 11. With C++ 11, cout is guaranteed to be initialized before other global variables. With older standard, you do have the Fiasco problem with cout.

  • @mariaemiliamachado5119
    @mariaemiliamachado5119 9 лет назад +8

    Great tutorials Bo, Congrats! Just a note, in C++11 you should use the nullptr keyword to initialize pointers instead of 0 or null for these operations.

  • @dreamjob3826
    @dreamjob3826 11 лет назад +2

    All videos on advanced C++ are excellent... Upload videos on more pitfalls and design approach to solve them. New topics also..

  • @ourdyingrepublic
    @ourdyingrepublic 10 лет назад +4

    You should not check if pointer is not equal to 0 before deleting it, standard guarantees that delete 0 will do nothing.

    • @azadalishah2966
      @azadalishah2966 5 лет назад

      ourdyingrepublic, I believe calling delete is more overhead as compared to checking if()

  • @ldxyz-s1e
    @ldxyz-s1e 8 лет назад +7

    I believe, the order of initialization depends on order of linkage.

  • @Nafiganado
    @Nafiganado 6 лет назад +3

    It does not look very intuitive for a user that he has to create an instance of singleton just to ensure its destructor is called...

  • @tematum
    @tematum 11 лет назад +2

    In the last example (6:35) you remove the meow call from the dog constructor - if you would not do that - there would be an infinite constructor calls for both cat and dog:
    So, if meow is called in the dog constructor - the cat is null so cat constructor called - cat constructor calls bark - the dog is still null so dog constructor is called again etc...
    Is there a way to deal with this? (maybe with some additional variable to check for a partially constructed class?)

    • @azadalishah2966
      @azadalishah2966 5 лет назад

      tematum , think about two normal functions each calling the other? It will create a loop. Same with constructor calling other constructor which calls back the caller, it will create infinite loop until stack overflow happens and program terminate.

  • @sonyolcu23
    @sonyolcu23 11 лет назад

    No, there would not be an infinite constructor calls even there would be meow call in the dog constructor. You missing something: after ..cat constructor calls bark- the dog is is not null because before program comes that point, main function called Singleton::getDog() and there is a static dog pointer initialized with a new dog object. In the Cat constructor the initialized dog object returned. Be carefull by the 'static' declaration.

  • @tqf9501
    @tqf9501 8 лет назад

    Hi! Thank you very much for your great tutorials!
    I have a question about this design pattern..
    Wouldn't it be safer to have an argument in the constructor of both classes so that we are sure the objected has been created?
    In that case we make sure that the object we want to make, will not be initialized unless we pass the correct input arguments.
    For example the Cat class would become:
    Cat::Cat(char* name, Dog* dog)
    {
    cout

  • @tomwellington4255
    @tomwellington4255 2 года назад

    Is there no need for any include statements in main.cpp

  • @Robjohn37
    @Robjohn37 8 лет назад

    Why is there an extern Dog d but no extern Cat c("Smokey") ?

    • @ldxyz-s1e
      @ldxyz-s1e 8 лет назад

      Because 'Cat c' is declared and used in same source.

    • @eduardmedla4243
      @eduardmedla4243 7 лет назад

      Dog d in cat.cpp is used from the main.cpp

  • @kocho4242
    @kocho4242 7 лет назад

    getCat and getDog functions can be implemented even simpler, with following code:
    static Dog& getDog()
    {
    static Dog dog;
    return dog;
    }

  • @azadalishah2966
    @azadalishah2966 5 лет назад

    😩Surprisingly my C++ 14 gnu compiler works without crashing when I use global variables in both dog n cat classes’s constructor and do interchange etc. I can’t even reproduce the error you found before using singleton. Program runs perfectly fine

  • @thachnnguyen
    @thachnnguyen 2 года назад

    Not a good example of why one should use singleton. In fact, not sure if static objects should ever be created.

  • @thestarinthesky_
    @thestarinthesky_ Год назад

    I define Meyer's Singleton so we don't need to have member data variables of static types:
    #ifndef Singleton_H
    #define Singleton_H
    #include "Dog.h"
    #include "Cat.h"
    class Singleton
    {
    public:
    static Dog* getDog();

    static Cat* getCat();

    static void cleanDog();

    static void cleanCat();

    private:
    Singleton() = default;
    };
    #endif
    #include "Singleton.h"
    Dog* Singleton::getDog()
    {
    static Dog* pDog = nullptr;
    if (pDog == nullptr)
    {
    pDog = new Dog("Gunner");
    }
    return pDog;
    }
    Cat* Singleton::getCat()
    {
    static Cat* pCat = nullptr;
    if (pCat == nullptr)
    {
    pCat = new Cat("Smokey");
    }
    return pCat;
    }
    void Singleton::cleanDog()
    {
    Dog* pDog = Singleton::getDog();
    if (pDog != nullptr)
    {
    delete pDog;
    pDog = nullptr;
    }
    }
    void Singleton::cleanCat()
    {
    Cat* pCat = Singleton::getCat();
    if (pCat != nullptr)
    {
    delete pCat;
    pCat = nullptr;
    }
    }

  • @sergeydubovtsev2472
    @sergeydubovtsev2472 11 лет назад

    Why do you sure that object "cout" is initialized befor your global objects?
    Ok, it was. But you say nothing about that:)

  • @dosomething3
    @dosomething3 7 лет назад

    Wow. What a terrible video. Bo, you should probably remove this video.