Forum Replies Created

Viewing 15 posts - 1 through 15 (of 25 total)
  • Author
    Posts
  • in reply to: inline static member #636724
    Antonella Ritorto
    Participant
        Up
        0
        Down
        ::

        I would like to extend the discussion with this example:

        Local static vs. Class data member static

        Situation 1: If my program has many objects of class A, at the same time, as many calls of print1(), then we could say that we prefer string1_ to be a static data member instead of a local static. Is this correct?

        Situation 2: If my program has only a few objects of class A, but call many times print2(), then we would say that we prefer string2 to be a local static, defined inside print2(), instead of a static data member. Is this correct?

        Situation 3: No matter how many objects of type A my program has, if I never call print2(), the local static is never initialized, so we gain something in this case (if we compare with the situation where string2 is also a static data member instead of a local one).

        Extra question: Do we need to add constexpr? Do we need to add inline also in the local static definition?

        in reply to: Sorting an array with std::set #636722
        Antonella Ritorto
        Participant
            Up
            0
            Down
            ::

            There is also std::to_string to convert integral types into string. It seems that you need the other way around, e.g. double to string. Can you somehow transform string into array of chars and then those somehow get converted into integral types? I haven’t read (yet) the details of your example though.

            in reply to: Antonella Ritorto #636721
            Antonella Ritorto
            Participant
                Up
                0
                Down
                ::

                Hi Paul! Well, I see it as another challenge. It’s not about being the strongest, the fittest, etc, rather discovering which muscles suit better for this and that movement (which tools, classes, methods, …),  discovering when to do this and that (in which piece of code should I add this or that?), how to manage your energy (handle memory, etc), being aware of your limitations (working with legacy code and external “rules”), and iterating as many times as you need to reach the desire “performance” (- no extra comment here! – ).

                I would say that aerial silk (or any sport?) can help in the sense of sharing the “same” problem-thinking spirit as in programming or math :)

                Antonella Ritorto
                Participant
                    Up
                    2
                    Down
                    ::

                    I can comment that

                    • we should prefer smart pointers to raw pointers. There are a few occasions when “raw pointers” are a must (which unfortunately do not come into my mind now, so it would be good to discuss/recall that), as you already mentioned (“… better dynamic memory allocation”)
                    •  we should prefer unique_ptr to share_ptr
                    • there is a useful table related to the Rule of 0/5/6 that helps us to identify when we can/must define the 6 (or not), or how defining only a few of them affects the code, for example, user-define the move constructor and move assignment and not the copy ones, etc.
                    Antonella Ritorto
                    Participant
                        Up
                        0
                        Down
                        ::

                        I saw that my question is related to an exercise regarding public static non-const data members of a class (with thread-safe interface). It’s mentioned that, in this example, the static non-const data member mem changes without synchronization. It’s suggested to put those modifications inside a public member function. I tried two things:

                        1. [Extra line] Just add a line inside interface1()  and interface2() definitions: mem = 10; mem = 20; respectively.
                        2. [Extra public member function] Adding a changeMem() public memeber function.

                        In both cases, I get data race (using -fsanite=thread). I haven’t spent so much time yet, but it would be cool to know how to get rid off those data races. Or it could be that I misunderstood the suggestion too. Thanks in advance!

                         

                        in reply to: Scoped Locking example – unnecessary headers? #631275
                        Antonella Ritorto
                        Participant
                            Up
                            0
                            Down
                            ::

                            Thanks! Apologize for the basic question, too. Then my conclusion, so far, is:

                            #include <iostream> // to use std::cout
                            #include <mutex> // to use std::mutex
                            #include <new> // to use std::bad_alloc
                            #include <string> // to use std::string
                            #include <utility> // (still trying to figure out where it’s needed)

                            in reply to: Scoped Locking example – unnecessary headers? #631273
                            Antonella Ritorto
                            Participant
                                Up
                                0
                                Down
                                ::

                                Thanks, Rainer! Then I have another question. Why do we need those headers? <new> and <utility>. Do we also need <exception>? Thanks in advance!

                                in reply to: abstract class #629236
                                Antonella Ritorto
                                Participant
                                    Up
                                    0
                                    Down
                                    ::

                                    I just found out in your example bridge that the abstract class there is used to be able to create either an object of type ImplementorA or ImplementorB. The constructors are defined (by default) in each derived class (ImplementorA/B), “instead of having a pure virtual constructor in abstract class Implementor, and having to override it in each class”.

                                    Just one more question, aren’t we declaring (not defining) an Implementor (by reference) in line 45 [Implementor& implementor;]?

                                    Can I conclude then I can declare but not define objects of an abstract class?

                                     

                                     

                                    in reply to: template parameter + enable_if #629225
                                    Antonella Ritorto
                                    Participant
                                        Up
                                        0
                                        Down
                                        ::

                                        Maybe I omitted some details. In this case, your code it’s a solution. My example is not the best since the real problem would look like:

                                        if ( “T == bar”){ // do something that cannot be done when “T != bar”, let’s say “doSth<T>();”}

                                        else { // do something else}

                                        then, the compiler wants to instantiate the complete definition when, T!=bar, but doSth<T>() is not-defined when T!= bar. That’s why I wanted a solution splitting the different definitions, using std::enable_if_t.

                                        Hope this helps, or maybe it makes it even worse :/

                                        in reply to: Factory method as static member of base class #629197
                                        Antonella Ritorto
                                        Participant
                                            Up
                                            0
                                            Down
                                            ::

                                            I think there was a typo in the previous link (the argument in createWindow3 had a reference, and it was supposed to be just a value),

                                            previousExampleTypoCorrected

                                            One of the error messages is:

                                            error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&)

                                            I’m not sure I understand. Could it be that passing the argument as a value, instead of reference or pointer, would (attempt to) increase the owners and that’s not how unique pointers are implemented?

                                            in reply to: Initialization of Raw Strings Literals #629108
                                            Antonella Ritorto
                                            Participant
                                                Up
                                                0
                                                Down
                                                ::

                                                I’m not sure I understand the concept of “delimiter”. I concatenated two raw strings to see if the “delimiter” means that some “extra space/limit” will be created between them, but it’s not the case. Could you please elaborate on the concept of delimiter? In your example, it’s TRENNER, in mine, HOLACHAUHOLACHAU. I don’t understand what this delimiter does.

                                                https://godbolt.org/z/8Px6G8njv

                                                in reply to: User-defined literal, overload operator #629090
                                                Antonella Ritorto
                                                Participant
                                                    Up
                                                    0
                                                    Down
                                                    ::

                                                    I have an additional question to my original one in this post. It’s about the keyboard friend. In the example provided with the class MyDistance, friend-keyword is used so that those overloaded (global) basic arithmetic operations can be done for MyClass objects.

                                                    My question is about the correct use of friend. Apart from the use-case shown in the example,  overloading global (arithmetic) operators in a class, can we also say that we would use friend to overload a method in a class A from another  class B? Not related by any hierarchy (no base/derived classes). What is the advantage/disadvantage of using the friend keyword instead of, in this case, re-defining the (“new”) method also in class A?

                                                    in reply to: Initialization of Raw Strings Literals #629089
                                                    Antonella Ritorto
                                                    Participant
                                                        Up
                                                        0
                                                        Down
                                                        ::

                                                        I have a similar question so I’m adding it here. I hope that’s okay.

                                                        What’s the difference between these two raw strings? What does TRENNER stand for? Is it something the user chooses? The outcome is the same for both.

                                                         

                                                        #include <iostream>
                                                        
                                                        int main(){
                                                        
                                                            // including \t \n
                                                          std::string raw1 = std::string(R"(C:\temp\newFile.txt)");
                                                          std::cout << "\n" << raw1 << "\n";
                                                        
                                                          // including \t \n and using delimiter
                                                          std::string raw2 = std::string(R"TRENNER(C:\temp\newFile.txt)TRENNER");
                                                          std::cout << "\n" << raw2 << "\n"; 
                                                        }
                                                        
                                                        in reply to: Return preserves const? #543963
                                                        Antonella Ritorto
                                                        Participant
                                                            Up
                                                            0
                                                            Down
                                                            ::

                                                            Is it a possible fix (alternative to the given solution in week9) to define val above the makeLambda() defintion? In that way, I “only” get a warning [capture of variable ‘val’ with non-automatic storage duration], so it’s a 100% fix, but it “still works”.

                                                            #include <functional>
                                                            #include <iostream>
                                                            #include <string>
                                                            
                                                            const std::string val = "on stack created"; // is it now created on the heap? 
                                                            
                                                            std::function<std::string()> makeLambda() {
                                                              return [&val]{return val;};
                                                            }
                                                            
                                                            int main(){
                                                            
                                                              auto bad = makeLambda();
                                                              std::cout << bad();
                                                            
                                                            }
                                                            
                                                            in reply to: compile vs run time #542488
                                                            Antonella Ritorto
                                                            Participant
                                                                Up
                                                                0
                                                                Down
                                                                ::

                                                                Thanks for the explanation. I need sometime to get into it. Thanks!

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