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'm trying to write the following code:
class MyClass{
public:
virtual int operator()()=0;
}
int bar()
{
return 1;
}
int _tmain(int argc, _TCHAR* argv[])
{
class : public MyClass{
int operator()(){
return 1;
}
} foo;
}
What's difference between foo and bar?
bar is a function, but foo is a function object. So if (for example) you'll write something like:
std::for_each(myContainer.begin(), myContainer.end(), foo);
(see this)
then your overloaded MyClass::operator() will be called for every single object in your container. The main advantage of function objects in comparison with function pointers is that they can be nested, it means that you can define your function class anywhere, including another classes or even methods. Moreover, it is ideal for usage in template functions, because Function can be template type.
Related
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 2 years ago.
Improve this question
This is my class
#include <vector>
class MyClass
{
public:
void print(); // prints X to console
private:
std::vector<int> X (4, 100);
};
When I call the print() method I want X to be passed by constant reference to print(). How can I do that? Currently is looks like as X is passed by value.
Ok, maybe this will help you to start thinking in c++, even if you did not make clear explanation what you need, if I may guess, what you probably think of is that you want to be able to call print() member function on const instances of MyClass, or rvalue has been bound to const lvalue reference, so you need to mark print as const member function of type MyClass.
class MyClass
{
public:
void print() const; // prints X to console
private:
std::vector<int> X (4, 100);
};
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'm creating class that has a templated object (Item<T>) as a member, basically like this:
class myClass
{
int other_int;
public:
int member_function();
vector<Item<T>> vec;
};
Currently, I have Item<string>, but I need to be able to use it with non string objects. Is there a way to do this without templating myClass (which would obviously be a lot of work for a complicated class)?
If your class will only use Item< string>, you may try:
class myClass
{
int other_int;
public:
int member_function();
vector<Item<string>> vec;
};
But if you want any other type of Item in the vector, the answer is No, there is no magic solutions.
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
How to Interpret the name of class in following piece of C++ code ?
Following code is part of a project which is compiling successfully with g++ compiler.
class ABC::DEF
{
public :
int a;
void func();
};
void ABC::DEF::func() { a = 3; }
ABC::ABC() : OBJ(new DEF())
{
}
ABC::~ABC()
{
delete OBJ;
}
How to interpret ABC,DEF and OBJ in the above code?
How the constructor defined above works ?
The definition of ABC most likely looks something like this:
class ABC
{
public:
ABC();
~ABC();
private:
class DEF;
DEF* OBJ;
};
and what you're looking at is the definition of the class ABC::DEF and the constructors of ABC.
(This is a quite normal way of implementing the "pimpl idiom".)
ABC::DEF is a nested class. If you look at the definition for class ABC, you should see a forward declaration of class DEF. You can give the full definition for such a class outside of the outer class as shown in your question.
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'm trying to improve my generic architecture, and I've decided that a system that uses this type of ambiguous context referencing would be ideal for my purposes. However, I'm having trouble figuring out how to get the syntax to work. I don't even know if something like this is possible! I have slightly shiestier alternatives that accomplish mostly the same thing but this would be best:
class IContained
{
public:
virtual int getInt() = 0;
};
typedef std::shared_ptr<IContained>IContainedPtr;
template<template<class RefType, RefType& itsRef> class ContainedType>
class TestClass
{
TestClass() :
myContained(new ContainedType < TestClass, *this>())
{
}
int getContextInt()
{
return 3;
}
IContainedPtr myContained;
};
template<class RefType, RefType& itsRef>
class Contained:
virtual public IContained
{
int getInt()
{
return itsRef.getContextInt();
}
};
TEST(POTATO, PARTY)
{
TestClass<Contained> myTest();
int thing = myTest.myContained->getInt();
EXPECT_EQ(thing, 3);
}
I am not sure what do you want to implement but I can explain why you are not able to use
*this
as template argument. Templates provide you with compile-time polymorphism. It means that all templates arguments should be known at compile time.
this
is a class instance variable, is an address of class instance, so it could not be determined during compilation. The same as
*this
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 9 years ago.
Improve this question
Imagine that you have a situation like this:
class className{
...
}
className func(){
className cl;
...
return cl;
}
int main(){
...
func();
}
What does the function func() return when you call it in the body of the program? A temporary copy of the object cl?
I don't understand this, since in the body of the function func() you can get the address &cl, but you get an error if you try to call &(func()) inside the function main().
Inside the function you are dealing with a so-called lvalue shortly speaking with an object which address is known because the object is defined explicitly.
The return value of the function is a temporary object (it is a so-called rvalue). Its address is not known. We do not know
where the compiler defined this object. So we may not apply operator & to a temporary object.
Another similar example
struct A
{
int x;
};
A f() { return A(); }
int main()
{
f().x = 10; // here the compiler will issue an error
}
This code shall not be compiled though for example MS VC++ 2010 will compile it due to either a bug or its language extension.:)