Forum Replies Created

Viewing 15 posts - 1 through 15 (of 37 total)
  • Author
    Posts
  • in reply to: Question about covariant return types #628632
    Avatardiana
    Participant
        Up
        0
        Down
        ::

        Isn’t the same happening in the solution to the exercise with smart pointers where we are returning std::unique_ptr<Interface> in the derived classes, hence not using the covariant return type?

        #include <iostream>
        #include <memory>
        #include <string>
        
        class Interface {
         public:
            virtual std::unique_ptr<Interface> clone() const = 0;
            virtual std::string getName() const = 0;
            virtual ~Interface() = default;
        };
        
        class Implementation1 : public Interface {
         public:
            std::unique_ptr<Interface> clone() const override {
                return std::unique_ptr<Interface>( new Implementation1(*this));
            }
            virtual std::string getName() const override {
                return "Implementation1::getName";
            }
        };
        
        class Implementation2 : public Interface {
         public:
            std::unique_ptr<Interface> clone() const override {
                return std::unique_ptr<Interface>( new Implementation2(*this));
            }
            virtual std::string getName() const override {
                return "Implementation2::getName";
            }
        };
        
        int main() {
            Implementation1 my_impl1;
            std::unique_ptr<Interface> my_clone = my_impl.clone();
            //error:
            //std::unique_ptr<Implementation1> my_clone = my_impl.clone();
            
        
        }
        
        in reply to: Strategized locking – Runtime vs. compile time #628626
        Avatardiana
        Participant
            Up
            0
            Down
            ::

            I see now that this is discussed in the Modernes post about Null Object:

            RuntimeCompile time
            Advantages– Allows it to configure the locking strategy during run time
            – Is easier to understand for developers who have an object-oriented background
            – Has no abstraction penalty
            – Has a flat hierarchy
            Disadvantages– Needs an additional pointer or reference indirection
            – It may have a deep derivation hierarchy
            – It may generate very wordy error messages

            So a (perhaps too) simplistic summary: compile time for performance and runtime for readability.

            in reply to: Safe variant example of modifyVector #571284
            Avatardiana
            Participant
                Up
                0
                Down
                ::

                In the real example of std::vector<T,Allocator>::push_back which has the strong exception guarantee: Wouldn’t you have to catch the exception that the allocation may throw by putting the push_back in a try except block?

                in reply to: Non-virtual template method #558242
                Avatardiana
                Participant
                    Up
                    0
                    Down
                    ::

                    Update: I just watched the video following the example about the non-virtual interface idiom 🙂 So I see that indeed processData doesn’t have to be virtual

                    in reply to: passing by smart pointer vs reference #555330
                    Avatardiana
                    Participant
                        Up
                        0
                        Down
                        ::

                        In Imran’s example the references are non-const since the functions modify the objects they are passed. What is then the modern approach for func1 and func2?

                        I don’t understand what passing ownership means.

                        in reply to: passing by smart pointer vs reference #554379
                        Avatardiana
                        Participant
                            Up
                            0
                            Down
                            ::

                            I prefer passing by reference because it expresses that the expectation is that the parameter is non-null.

                            With shared_ptr there is also the overhead of the control block which may or may not be a concern depending on the context.

                            Avatardiana
                            Participant
                                Up
                                1
                                Down
                                ::

                                Thanks! I started watching the AM session in the meantime. The discussion was very interesting.

                                in reply to: Pointer to object to decorate #550403
                                Avatardiana
                                Participant
                                    Up
                                    0
                                    Down
                                    ::

                                    In both examples we are using reference semantics right since we are using Shape* and Shape&?  Or do raw pointers model something else?

                                    How would value semantics look like in decorator.cpp? Since we need the indirection via a pointer or a reference to get the polymorphic behavior for instance in the call shape.GetName() I don’t see how we can avoid references when using decorators.

                                    in reply to: Factory method as static member of base class #546038
                                    Avatardiana
                                    Participant
                                        Up
                                        0
                                        Down
                                        ::

                                        Thanks Rainer! I appreciate now the differences in the three approaches (ignoring the use of naked new vs. smart pointers) and referring to the diagram from refactoring guru

                                        1. factoryMethodClassic.cpp  : the creator class hierarchy is replaced by a single function
                                        2. factoryMethodUniquePtr.cpp : the creator class hierarchy is blended in with the product hierarchy (i.e. the create methods go directly in the product classes)
                                        3. The example in the refactoring Guru: the creator is its own class with concrete classes corresponding to the concrete products

                                        I don’t quite get this comment in the refactoring guru diagram:

                                        “Note, despite its name, product creation is not the primary responsibility of the creator. Usually, the creator class already has some core business logic related to products.”

                                        Is this referring for instance to the render() method in UI example?

                                        in reply to: Modeling ownership or borrowing #546037
                                        Avatardiana
                                        Participant
                                            Up
                                            0
                                            Down
                                            ::

                                            Thanks Rainer! I see also the other instance you describe in your reply to factory-method-as-static-member-of-base-class as another example of when one would use shared_ptr.

                                            in reply to: Instantiating with {} or = #542298
                                            Avatardiana
                                            Participant
                                                Up
                                                0
                                                Down
                                                ::

                                                Built-in types:

                                                I think there is no performance difference but the first one has the advantage of disallowing narrowing conversions.

                                                For std::string:
                                                std::string s {“Hello”}; //direct initialization
                                                std::string s = “Hello” //copy initialization
                                                I think there is no difference:
                                                Direct initialization: the constructor taking a const char* is called
                                                Copy initialization: the third bullet point here applies and so the string literal is converted to a prvalue expression of type std::string and then used to direct initialize the object but this last step is optimized away (second bullet point here) and the result of the conversion is constructed directly in the memory allocated for the target object.

                                                In general, I don’t know. I realize there are lots of details to keep in mind around initialization whenever I read cppreference pages related to initialization 😛

                                                in reply to: When to use std::move? #541748
                                                Avatardiana
                                                Participant
                                                    Up
                                                    0
                                                    Down
                                                    ::

                                                    The clang-tidy rule modernize-pass-by-value suggests to replace the use of const references constructor parameters that are copied into class fields with a pass by value that is then moved.

                                                    in reply to: Industries and projects #482091
                                                    Avatardiana
                                                    Participant
                                                        Up
                                                        0
                                                        Down
                                                        ::

                                                        Thank you all for your posts! Very interesting.

                                                        I work on the timing model of FPGAs. An FPGA is a device that has an undefined function at the time of manufacture, it is programmed on the field. A large component of the product is the software that is used to program the device and verify that it will function as expected. Part of that verification is making sure that data can be reliably transferred from register to register in the FPGA and between registers on the FPGA and other devices on the board, that’s where the timing model for the device comes in.

                                                        I am responsible for part of the data that is consumed by the timing model and I am currently restructuring how  we store, verify and query package parasitics and I am adding new functionality to support new packaging methodologies.

                                                        in reply to: Threads and Mutexes #457275
                                                        Avatardiana
                                                        Participant
                                                            Up
                                                            0
                                                            Down
                                                            ::

                                                            Related to Driton’s question #4:

                                                            In all the concurrency examples in weeks 28-30 the key variables (mutexes, condition variables, shared data) were declared global. Are the patterns, idioms or strategies to avoid the global variables?

                                                            in reply to: Ping pong exercise #457273
                                                            Avatardiana
                                                            Participant
                                                                Up
                                                                2
                                                                Down
                                                                ::

                                                                Thanks Driton! Yes this is one instance where I thought this could happen.

                                                                The part that I was missing is that this doesn’t result in a deadlock because as you say condVar1 releases the mutex, goes to sleep and waits for the notification. I agree one thread could acquire the lock at most twice in a row since the second time around the wait call fails and the thread is blocked until the other thread returns the ball.

                                                                It could happen multiple times I think, just each time at most twice in a row, for instance if for some reason the call to lock in the last row of the table below (vertical axis is time) gets delayed and t1 has a chance to acquire the lock again before t2 gets a chance to do so. Ultimately the ping pong functionality remains the same with the two threads taking turns in changing the value of the variable.

                                                                t2(set_false)t1(set_true)
                                                                lock
                                                                wait success
                                                                set variable to true
                                                                notify
                                                                lock
                                                                wait fail
                                                                unlock
                                                                lock
                                                                wait success
                                                                set variable to false
                                                                notify
                                                                wait success
                                                                lock
                                                                set variable to true
                                                                lock

                                                                 

                                                              Viewing 15 posts - 1 through 15 (of 37 total)