C# extension methods analogue in C++ [duplicate] - c++

This question already has answers here:
Extension methods in c++
(7 answers)
C++ Class Extension
(9 answers)
Closed 5 years ago.
C# has this little nice feature: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods
This is really cool. Let me give you an example:
I want to add concat method to std::vector but I don't want to inherit it. This feature would be very useful. Do you have any analogues feature in C++ that allows to add a function to a type without inheriting from the original type? I am asking for a language-level feature, please.

In C++ this is not directly possible; that being said, you could implement a function that has a reference to a std::vector as a first argument, serving a similar purpose as an extension method (which more or less are just syntactic sugar for that).

A free function might be your friend here e.g.
namespace VectorMethods
{
std::string contat(const std::vector<std::string>& vec)
{
// return result of concatenating vector
}
}

Related

Which one is best approach class or structs? In c++ [duplicate]

This question already has answers here:
When should you use a class vs a struct in C++? [duplicate]
(27 answers)
Closed 4 years ago.
Suppose you have to develop an application for XYZ bank with the following features.
1)The application must be secure
2)For transactions, proper interfaces will be provided to the customers.
3)The application must be reusable.
4)The application must be efficient in terms of speed and memory usage.
You can use either structs or class in order to achieve the above mentioned features. So which programming construct (class or struct) will you select for application development.
Please help me out here. Thanks
struct and class are nearly equivalent in C++ (you can have member functions, constructors & destructors, data members in both). More precisely,
struct Sometype {
/// some code here
};
is equivalent to
class Sometype {
public:
/// some code here
};
So the runtime efficiency is the same (since public: is an annotation for the compiler which is lost, as most type information, at runtime; be aware of type erasure).
You really should take days to read some good book about C++ programming, then look into some C++ reference site, then read (or at least refer to) some C++ standard like n3337 (for C++11; for later standards, find them by yourself).
Learn about the rule of five and about standard containers and smart pointers.

is overloading parantheses c++ a good idea [duplicate]

This question already has answers here:
What are C++ functors and their uses?
(14 answers)
Closed 4 years ago.
I have programmed in c++ for a couple of months, and I am starting to understand the core. At the moment I am trying to make a calender program with classes and inheritance (just to get more comfortable with object oriented programming), and a few weeks ago I learned about operation overloading.
I am wondering, is it a bad idea to overload parantheses for an object, such that I could for instance write this, or can an error occure becuase the compiler can confuse it for something else(constructor or something like that)?
//creating a valid year-object
Year year1998 = Year(1998,true);
// the parantheses operator returns a day(another object)
Day d = year1998(1,10);
//the overloading
Day& Year::operator()(int monthNumber, int dayNumber){
//Just returns a day from the month class
return months[monthNumber][dayNumber];
}
Overloading this operator is a basic concept in C++ and is extensively used in function objects, a typical example being comparison functions.
You can savely use this operator, but nowadays often lambda functions are preferred to function objects.

Confusion regarding char[] [duplicate]

This question already has an answer here:
Should statically-declared character arrays with a specified size be initialized with a literal in C?
(1 answer)
Closed 8 years ago.
I have recently started programming in C++ and that's why I am facing lots of confusions.
I wanted to know what's wrong in using the following piece of code:
char interface[20];
interface="USB01";
The C++ language does not allow you to assign to arrays.
You can, however, initialize arrays. The syntax is similar, but the assignment operator is used in the same statement as the declaration:
char interface[20] = "USB01";
However, in C++, one would typically use a standard container like std::string rather than C strings. These are far easier to use and do allow for natural assignments.
std::string interface;
....
interface = "USB01";
Note that we don't need to decide up front how much space to reserve for the string. This is just one of the many benefits of using the standard string class.
You can't assign arrays like that, but you can initialise them:
char interface[20] = "USB01";
In C++ though you should be using proper C++ strings, i.e. std::string, not C-style char * strings:
std::string interface;
interface = "USB01";

When do we need to use a structure in a pure C++ program? Is structure at all required in a pure C++ program? [duplicate]

This question already has answers here:
When should you use a class vs a struct in C++? [duplicate]
(27 answers)
C++ - struct vs. class [duplicate]
(5 answers)
Closed 9 years ago.
When do we need to use a structure in a pure C++ program? Is structure at all required in a pure C++ program?
I understand that structure is the only way in a C program to encapsulate members and function pointers. But, if I use C++ to code my complete module, is it 100% alright if I stay away from structures and use a Class instead? Am I missing something?
(I googled but my question is very specific - is structure useless in pure C++ code)
=== EDITED ===
My question is not about the difference or commonalities between a structure and a class, but, do we need "struct" at all in C++, when you can 100% do away with a class. Is structure not redundant construct in C++, except for backward compatibility with C? I understand the difference between them w.r.t. the access specifiers but there are only syntactic differences.
The struct has been kept in C++ for backward compatibility with C. By default the struct members are public but the class members are private. You can replace class and struct with each other and get the same result if the type of members are pre-defined.

finding addressof a object whose ampersand operator is overloaded and private [duplicate]

This question already has answers here:
If an operator is overloaded for a C++ class how could I use a default operator instead?
(3 answers)
Closed 8 years ago.
I have a class which is overloading ampersand(&) operator and made it private. I don't have a latest C++11 compliant compiler so is there any way by which I can get address of my object using current C++ compiler only
Thanks
reinterpret_cast<T *>(&reinterpret_cast<char&>(obj))
Dunno if it's safe though.. (well clearly it's a bit dodgy)
Use the addressof() function from Boost.Utility. If you don't want to use Boost, you can still look at it's implementation which consists of just a single header.