- About placing member variable into a pure virtual base class - 2 Updates
- Available C++ Libraries FAQ - 1 Update
- What is this constructor? - 2 Updates
Thiago Adams <thiago.adams@gmail.com>: Oct 14 02:44PM -0700 On Sunday, October 14, 2018 at 4:11:26 PM UTC-3, Öö Tiib wrote: > it is best using visitor pattern and sometimes it is best using > variants. I still do not understand in what context your idea is better > from those others. It gives some benefits of visitor. I have to think more about the advantages over visitor, but I think it is simpler and faster. I am using this pattern (not the C++ version, but the C one, with int as tag and switch cases) to implement an AST data structure. Sometimes we have two or more types of objects inside the same container. It doesn't mean that these types have something in common. Maybe the only think they have in common is that they are used together. Lets say you have a container of 'car parts'. Them we have glass and tires. What do they have in common? In this case, using interfaces we may have IObject and the container vector<IObject>. But IObject is two generic. I can have glass or tires, but I cannot have fruit. If we had ICarPart then the glass or tire is very coupled with the original problem and is difficult to reuse and confuse as well. In the AST, one sample is static_assert it can be in different positions without any relationship with the other parts. I will consider this solution in many other cases, where I am the owner of the types. (I know all types) > seem more rare than other types. Also, if you have to have virtual > functions and have to check typeid through vtable then how it can be > better than just to use the virtual functions? I need the RTTI and the virtual destructor was needed to use unique_ptr in this sample. The sample is avoiding coupling cause by interfaces. I am not using this dynamic_select in any program, but I am using the 'dynamic switch selection' in a C program. |
Thiago Adams <thiago.adams@gmail.com>: Oct 14 03:31PM -0700 On Sunday, October 14, 2018 at 6:44:59 PM UTC-3, Thiago Adams wrote: ... > I cannot have fruit. If we had ICarPart then the glass or tire > is very coupled with the original problem and is difficult to reuse > and confuse as well. and just to show how far this can go.. Imagine you have a container of airplane parts, then you can have glass, tires and wing. glass, tires can be at car parts or airplane parts, but wing can be only at airplane parts. Considering this, then ICarPart stopped to make sense. and here we go again trying to find sets of interfaces. If we use IObject this doesn't represents well the possible objects that the container can have. |
Nikki Locke <nikki@trumphurst.com>: Oct 14 10:23PM Available C++ Libraries FAQ URL: http://www.trumphurst.com/cpplibs/ This is a searchable list of libraries and utilities (both free and commercial) available to C++ programmers. If you know of a library which is not in the list, why not fill in the form at http://www.trumphurst.com/cpplibs/cppsub.php Maintainer: Nikki Locke - if you wish to contact me, please use the form on the website. |
Sam <sam@email-scan.com>: Oct 14 04:27PM -0400 > ~A(); > }; > What is the first constructor doing? Thanks. The first constructor takes an optional int parameter. If not passed, the default value of the int parameter is (A(5), 0) This expression first construct a temporary A object, this time passing the first parameter as 5, then this gets destroyed by the comma operator, and the whole expression evaluates to 0, thus constructing the object with the int parameter's value set to 0. So, if this class's constructor is not supplied with a value, a temporary instance of this class gets constructed using the parameter value of 5, this temporary instance of this class gets destroyed, then the actual instance gets constructed with the int value parameter of 0. Why this is done this way, only the devil knows. |
Chris Vine <chris@cvine--nospam--.freeserve.co.uk>: Oct 14 10:58PM +0100 On Sun, 14 Oct 2018 16:27:48 -0400 > first parameter as 5, then this gets destroyed by the comma operator, and > the whole expression evaluates to 0, thus constructing the object with the > int parameter's value set to 0. On a minor quibble: the lifetime of the temporary is not destroyed by the comma operator. According to §12.2/3 of C++14 "Temporary objects are destroyed as the last step in evaluating the full-expression (1.9) that (lexically) contains the point where they were created". According to §1.9/10 "A full-expression is an expression that is not a subexpression of another expression". This includes the entire expression (A(5), 0). I think it also includes the lifetime of the constructor of the A object which is initialized by this expression, because any temporaries created on evaluating the arguments of a constructor or other function last during the execution of the function. This would seem to be confirmed by the code below which prints: In constructor with with value 5 In constructor with with value 0 Destroying object with value 5 Destroying object with value 0 #include <iostream> struct A{ int i_; A(int i = (A(5),0)) noexcept : i_(i) { std::cout << "In constructor with with value " << i << std::endl; } A(const A&) noexcept {} A(A&&) noexcept {} ~A() { std::cout << "Destroying object with value " << i_ << std::endl; } }; int main () { A a; } |
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page. To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com. |
No comments:
Post a Comment