how to declare a generic class in c++ [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
My main question is: can I put the generic type before a class definition?
Like, can I do something like this:
generic class Classname (ParameterType parameter)
{
cout << "Hello world";
}

Templates are the way to do generics in C++. You can write it like this:
template<class ItemType> class ClassName
{
public:
ClassName(const ItemType& newdata) : data(newdata) {}
private:
ItemType data;
};
Later on in main:
ClassName<int> data1(1);
ClassName<char> data2('A');

Related

How to implement c++ template<T> in golang [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How to translate this cpp code to golang ?
template<T> class CppTemp {
T a;
T* pa;
T foo(T &t);
};
template<T> T foo2(const T &t)
Go doesn't support templates or generics. There are three things you can do:
use non-empty interfaces where applicable
generate code with go generate
use interface{}:
type GoTemp struct {
a interface{}
}
func (gt *GoTemp) foo(v interface{}) {
// ...
}
func foo2(v interface{}) {
// ...
}

How to override the setter? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an private property in my class and I want to add some code on its setter, how can I accomplish that?
I might do that using method but I don't want to just to avoid using property.
private:
float damage;
You can replace your plain old data type with a class type to do what you want:
class Damage
{
public:
Damage& operator=(float x) { value = x; /* add code here*/ }
operator float() const { return value; }
private:
float value;
};
Then just replace your float with Damage and you can make it behave how you want.

C++ variable type [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a function which takes two variables:
call((void**) &var, float);
call((void**) &var, int);
call((void**) &var, string);
Now I want to create a function to call that special function:
function start_call(/*what to put here?*/)
{
call((void**) &var1, float);
call((void**) &var2, int);
call((void**) &var3, string);
//code
}
So, what to put in the "what to put here?" place, in order for the function to accep any variable?
I tried my berst to explain it...
You could use a template:
template <class T> start_call(T in);

Coding convention of using a string literal [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I know there is no specific rule of how to use or declare a string literal, like for example in my class, I want to use "MyName" string literal, and its the only class that will use it, for example,
// CFoo.h
class CFoo
{
public:
CFoo();
~CFoo();
void printString();
}
// CFoo.cpp
CFoo::CFoo()
{
}
CFoo::~CFoo()
{
}
void CFoo::printString()
{
std::cout << "MyName" << std::endl;
}
Now I want that "MyName" will have a descriptive name placeholder, like NameLiter or something like that. Should I use define preprocessor, or declare it as global in cpp as const std::string? Or should I make a private member variable and initialize it in the ctor initializer list?
Thanks!
Making it a private static const char* in CFoo would satisfy your requirements.

How to use variable type Struct inside class? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have to build a videogame with SDL for my end year proyect. However I'm somewhat lost about how and when to use classes.
T tried to include a variable type struct inside a class but I can't manage to do it, maybe Position should be a class instead of a struct? Here's my code:
struct Position{
int x,y;
};
class Object{
private:
Position pos;
Position speed;
int tipe;
public:
Objeto(int,int);
Objeto();
~Objeto(); // DESTROY
};
When I try to do this I get error: 'class Object' has no member named 'x' How can I include the struct in the object?
From your error, I think you are trying to use
Objeto.x
instead of
Objeto.pos.x