Forum Replies Created

Viewing 15 posts - 271 through 285 (of 349 total)
  • Author
    Posts
  • in reply to: Friendship and inheritance #78642
    RainerRainer
    Keymaster
        Up
        0
        Down
        ::

        Friendship should only be granted exclusively. The following rules hold:

        class A {
            friend class B;
        };
        
        class B {
            friend class C;
        };
        
        class C {};     // 1. friendship is not transitive
        
        class D : B {}  // 2. friendship is not inherited
        
        1. Friendship is not transitive: class B is a friend of class A, and class C is a friend of class B ⇒ class C is not a friend of class C
        2. Friendship is not inherited: class B is a friend of class A, and class D derives from class B ⇒ class D is not a friend of class A

        The same rules apply to functions. ⇒ Friendship is not inherited.

        in reply to: Wong video for Create and Delete #77234
        RainerRainer
        Keymaster
            Up
            0
            Down
            ::

            Thanks. I fixed it.

            in reply to: Example code memberFunctionVirtual.cpp #75555
            RainerRainer
            Keymaster
                Up
                0
                Down
                ::

                Thanks: Line 51 should be aRef.withdraw(150). I fixed it.

                in reply to: Placement new #75030
                RainerRainer
                Keymaster
                    Up
                    0
                    Down
                    ::

                    You are right. Due to the fact that the constructor runs automatically, the initialization of the memory with new is an overkill.

                    in reply to: Mentee Membership and Community Membership #75029
                    RainerRainer
                    Keymaster
                        Up
                        0
                        Down
                        ::

                        Richard, you are right. But first, I want to provide value to my mentees. Second, I’m not sure about the fair price.

                        • If I make it too cheap, my current mentees may be annoyed.
                        • If I make it too expansive, no one would participate.
                        in reply to: Implementation inside class or not? #74782
                        RainerRainer
                        Keymaster
                            Up
                            0
                            Down
                            ::

                            This is mainly a matter of taste of how you structure source code. There are a few subtle differences:

                            • When you define a member function inside the class, it becomes automatically inline.
                            • The syntax becomes pretty complicated when you define a generic member function outside a class template. The member function has to specify the class template declaration and the member function declaration.

                            template <typename T, int N>
                            class Array{
                             public:
                                template <typename T2>
                                Array<T, N>& operator = (const Array<T2, N>& a);
                            };
                            
                            template <typename T, int N>
                                template <typename T2>
                                Array<T, N>& Array<T, N>::operator = (const Array<T2, N>& a) { }
                            
                            • When a template is instantiated, its definition must be available. Consequentially, you have to include the member function definition in the header.

                            // foo.h
                            
                            template <typename T>
                            struct Foo{
                                void func(T param);
                            };
                            
                            #include "foo.cpp"
                            
                            in reply to: Mentee Membership and Community Membership #74570
                            RainerRainer
                            Keymaster
                                Up
                                0
                                Down
                                ::
                                1. You will be a Community member for the time you are a Mentee member.
                                2. Thanks. I fixed it.
                                in reply to: Deriving privately #73649
                                RainerRainer
                                Keymaster
                                    Up
                                    0
                                    Down
                                    ::
                                    • Deriving publicly means that the derived class is a base class. Therefore, you can use the derived class in a function call when a base class is required. The derived class is a base class.
                                    • Deriving privately means that the derived class can only use public and protected members of the base class. The derived class uses the base class for its implementation.

                                     

                                    in reply to: Calls to delete in operatorOverloadingAssignment.cpp #69269
                                    RainerRainer
                                    Keymaster
                                        Up
                                        1
                                        Down
                                        ::

                                        struct MyType{};
                                        
                                        • Copy constructor and move constructor
                                        MyType my1;
                                        
                                        MyType my2(my1);            // copy constructor
                                        MyType my3(std::move(my1)); // move constructor
                                        

                                        Steps of copy and move constructor:

                                        1. Create yourself from the other
                                        2. Special step
                                          1. Move Constructor: put the other in a valid but unspecified state
                                        • Copy assignment operator and move assignment operator
                                        MyType my1;
                                        MyType my2;  
                                                 
                                        m1 = m2;            // copy assignment operator
                                        m1 = std::move(m2); // move assignment operator
                                        

                                        Steps of copy assignment and move assignment operator

                                        1. Delete yourself
                                        2. Initialize yourself from the other
                                        3. Special step
                                          1. Copy assignment operator: check for self assignment
                                          2. Move assignment operator: put the other in a valid but unspecified state
                                        in reply to: Calls to delete in operatorOverloadingAssignment.cpp #68935
                                        RainerRainer
                                        Keymaster
                                            Up
                                            0
                                            Down
                                            ::

                                            Just put the Account‘s in an additional scope in main. Now, you see the expected behavior: https://godbolt.org/z/PYajYv14o

                                            in reply to: Design pattern to avoid nested if else blocks #68735
                                            RainerRainer
                                            Keymaster
                                                Up
                                                1
                                                Down
                                                ::

                                                This is a topic of my Design Pattern mentoring, but you can read the following post in which I compare the various techniques: https://www.modernescpp.com/index.php/c-core-guidelines-more-about-control-structures. 

                                                 

                                                in reply to: constexpr and non-constexpr constructors #68287
                                                RainerRainer
                                                Keymaster
                                                    Up
                                                    1
                                                    Down
                                                    ::

                                                    Here is an idea for a class having a constexpr and non-constexpr constructor

                                                    • Instances of your class should be creatable at compile time. => You make your constructors constexpr. Consequentially, you can use this constructor also at run time.
                                                    • Now, you need a constructor that performs some operations, which are only doable at run time. E.g.: invoke a logger. A logger cannot be constexpr because it logs the time. => You have a class with constexpr and non-constexpr constructors.
                                                    in reply to: cpp stories example #67954
                                                    RainerRainer
                                                    Keymaster
                                                        Up
                                                        0
                                                        Down
                                                        ::

                                                        Thanks. This is a failure in the slides. I will fix it.

                                                        in reply to: mutable attributes #66319
                                                        RainerRainer
                                                        Keymaster
                                                            Up
                                                            0
                                                            Down
                                                            ::

                                                            Threading and mutable have nothing in common. When you update a cache in a single threaded environment, there is no need for synchronization using a mutex.

                                                            in reply to: Constructor delegation #66317
                                                            RainerRainer
                                                            Keymaster
                                                                Up
                                                                0
                                                                Down
                                                                ::
                                                                1. It behaves such as the compiler first creates a representation of the class before it uses it. For the same reason it works that you can invoke a member function before it was declared: https://godbolt.org/z/h9djnP9oa
                                                                2. In the concrete case, initialization in the class body and the constructor is the same. In general, in class initialization is the default behavior of a class and constructor initialization is a special initialization.
                                                              Viewing 15 posts - 271 through 285 (of 349 total)