C++ :: Calling Method Of Specialized Template Base Class In Subclass
Feb 10, 2013
class IFoo {
virtual void Bar() = 0;
};
class FooAbstract {
virtual void Bar() {}
[Code] .....
How to call the Bar() method from FooTemplate in FooDerived::Bar()?
View 5 Replies
ADVERTISEMENT
Jan 22, 2015
PHP Code:
class B {
public:
template<class T>
T foo(){}
};
class D1:public B {
[Code] ....
I have that code piece and would want it to work but it doesn't.
Error "Error2error C2993: 'double' : illegal type for non-type template parameter '__formal' ....
I have no choice and have to use double and float for template typename when specializing. I tried to wrap it up like this
PHP Code:
typedef struct DOUBLE {
DOUBLE(double d){ val = d; }
double val;
};
but it's of no use.
View 6 Replies
View Related
Nov 2, 2014
how I want the code to look. Only problem is it doesn't work (Line 11). I have some experience with templates but I'm not a pro.
Basically I want the "Channels<3>" to be a type that I can use to specify a Cable with similar to vector<float/int> it would be Cable<Channels<2 or 3>>.
What have I messed up with the syntax?
#include <iostream>
#include <vector>
using namespace std;
[Code].....
View 4 Replies
View Related
Feb 2, 2015
I have a question similar to the one here: [URL] .....
The main difference is I would like to pass a method of derived class as a parameter to some other method in its template base class.
template <typename BaseType>
class Base {
public:
typedef void (Base::*Callback)(int A);
[Code] .....
The above is an example which does not compile. My compiler complains that the two BaseMethod() calls in DerivedMethod() are invalid uses of non-static member function.
Is this not possible to do, or is my syntax simply wrong? All I want is to be able to pass as an an argument to a method in the base class from the derived class some callback as a variable for the base class to invoke later.
View 2 Replies
View Related
Jul 19, 2013
I have a class "Result" with a single template function Set(const std::string& arName, T& val) and a specialization of this function Set<Real>(const std::string& arName, Real& val) where Real is a typedef for double. The class is in a shared library and I use it in my main program. If I do result->Set<GLOBAL::Real>("U", 100.0); the wrong template function is called!
I check this by the output with std::cout.
Maybe it's a problem with the typedef.
If I link the object file of the Result class directly to my main program (no shared library), it works.
typedefs.hpp:
namespace GLOBAL {
typedef double Real;
} results.hpp
#include <iostream>
[Code] ....
View 3 Replies
View Related
Jul 15, 2014
I would like to know if there's a way to make a method from a derived class a friend of its base class. Something like:
class Derived;
class Base {
int i, j;
friend void Derived::f();
protected:
Base();
[Code] ......
View 3 Replies
View Related
Mar 30, 2013
Say I have 3 classes:
class Player {
public:
virtual func1();
[code]....
Say in my main class, I have a function fight(Player p1, Player p2) and I would like to do something like this in the fight function, given that p1 is the human and p2 is the computer:
//function fight()
fight(Player p1, Player p2) {
p1.func2();
}
//using function fight()
fight(human, computer);
When I compile the program, I got this: error: ‘class Player’ has no member named 'func2()' What can I do to allow p1 to call func2 inside fight()? I'm not allowed to use pointers as the parameter for fight() and have to use the signature fight(Player p1, Player p2).
View 6 Replies
View Related
Mar 30, 2013
I'm having some difficulties in understanding the topic which I stated above.
View 5 Replies
View Related
Mar 23, 2014
I'm still working on my process API, as in my previous posts. Right now I'm trying get my class portable so I can use it for any language/compiler by using a factory design pattern. I'm having problems figuring out how to call the methods properly from my interface pointer in my factory class without causing a segmentation fault.
Code: main.cpp
Code: #include "exports.h"
#include <iostream>
using namespace std;
[Code]......
View 5 Replies
View Related
Aug 12, 2014
class intClass{};
class stringClass{};
template <typename T>
class Mediator {
// bunch of code
};
I want Mediator<int> to declare intClass friend, Mediator<std::string> to declare stringClass friend etc..., without having to redefine the entire specialization
template<>
Mediator<int>::friend intClass;
View 5 Replies
View Related
Jul 8, 2014
Code:
class A {
public:
template<class T>
ObjectsBase* createObject(T* objType, Ogre::String name, Ogre::Vector3 position);
};
template<class T>
ObjectBase* A::createObject(T* objType, Ogre::String name, Ogre::Vector3 position);
My goal is to create any ObjectBase* derived object and return it to class A....
View 6 Replies
View Related
Aug 1, 2014
Code:
bool CObject<class T>::Create(LPDIRECT3DDEVICE9 pDevice, T *pMesh, float fAnimSpeed) {
if(pMesh == NULL)
return false;
m_pDevice = pDevice;
m_pMesh = pMesh;
if(m_pMesh->GetAnimController() != NULL) {
DWORD dwNumAnimSet = m_pMesh->GetAnimController()->GetMaxNumAnimationSets();
In here, T can be from the class CMesh * or CSkinnedMesh *, both have signature of GetAnimController
This maybe a flaw in my design... But I want Create to accept any kinds of meshes.
Should I create an interface or abstract class above all of those?
I have added an IMesh class that every derived classes has to have the needed interface.
But since then, I have another question, I feel that it is very cumbersome to include a template argument in every class/prototype that is associated with CObject
Say
bool CreatePlanesFromObject(CObject *pObject, D3DXPLANE *pPlanes);
Requires me to do this
template<class T> bool CreatePlanesFromObject(CObject<T>* pObject, D3DXPLANE* pPlanes);
Any ways I can omit the template argument, because it doesn't make sense to know the Mesh type in other classes...
View 1 Replies
View Related
Jan 21, 2013
I want to specialize a particular function for Integer datatype inside a class template. My XX.h will be
namespace ZZ {
template <class T>
class XX {
void doSomething(T x);
};
}
provide me XX.cpp which has template specialization for the function doSomething on Integer datatype.
I tried the following in XX.cpp
#include "XX.h"
using namespace ZZ;
template <class T>
void XX<T>::doSomething(T x) {
[Code] ...
But this code is not working.
View 2 Replies
View Related
Feb 18, 2015
I have 2 classes with a Function with the same definition (both inherited from the same base class) and in my derived class I derive from both of those 2. Is it possible to use the Methods of both classes? for example with an other name?
class A {
protected:
int print(int a) { std::cout << "int A: " << a << std::endl; }
};
class B : A
[Code] ....
is there something like using C::print as printc;?
The Problem, I have a Sprite class that derives from a Rectangle with properties Position, Size, Origin and Angle and a Text class that derives from Rectangle. Now i have a Button class deriving from both Sprite and Text.
- The Position, when moving the Button i have to change the position of both so i Implemented a new Method which calls SetPosition from the Sprite and the Text.
- The SetSize just affects the Button so i just did using Sprite::SetSize;
- The angle affects both so i just implemented a new Method and hide the other two
The problem is here:
- The Origin: writing button.SetOrigin(-1,0) should set the Origin of the Button and writing button.SetTextOrigin should set the Origin of the text.
Should i just reimplement a Mehtod named SetTextOrigin and call Text::SetOrigin from there and hide the button.Text::SetOrigin or is there something like using Text::SetOrigin as SetTextOrigin?
View 6 Replies
View Related
Dec 24, 2014
Allow users to enter their name and favorite saying in a single method that gets invoked two times. If I can only return one value at a time, how am I suppose to get name and favorite saying out of UserInput()?
static void Main(string[] args) {
string displayName, favoriteSaying;
DisplayInstructions();
Console.WriteLine();//readability
[Code] ....
View 3 Replies
View Related
Oct 1, 2013
I would like to access (use) a (virtual) method declared in a base class via a pointer to an object belonging to a derived class, which overrides the base method. Is it possible? Up to now I have not been successful. I am including a program with a more detailed description.
// declare a base class T0 with virtual function P, and a derived
// class T1 which overrides P; declare two ptrs, to T0 and T1,
// and generate corresponding objects; calling P via the two
// ptrs gives expected result; however I would like to take
// ptr to T1 object & execute the base function (declared
// in T0); is it possible? I tried all possible casts but nothing
// works; no matter how I cast the pt1 ptr, I always end up executing
// the overriding function (declared in T1)
// actual output (debian 7, gcc 4.7.2-5):
[code]....
View 5 Replies
View Related
Oct 7, 2013
Im trying to create a map container with the key being an ID number and the value being a pointer to a class object. Currently Im creating objects and storing their address in the container. I am getting a runtime error when calling the virtual method with this pointer. I believe that the problem is being called because they aren't being called pointer/reference. let me know if you need more.
if(command == 'F'){
inputDataFile>>name>>mNumber>>email>>department>>tenure;
faculty newFaculty(name,mNumber,email,department,tenure);
person* facultyAdd = &newFaculty;
cout<<"Note: Adding "<<mNumber<<"..."<<endl<<"Adding ";
people.insert(pair<string,person*>(mNumber,facultyAdd));
[Code]...
View 3 Replies
View Related
Nov 12, 2014
how the operator overloaded methods called in C++?
Class String {
int len;
char *str;
public:
String(const char *str1="") {
len=strlen(str1);
str=new char[len+1];
strncpy(str,str1,len+1);
[code]....
View 1 Replies
View Related
Mar 1, 2013
I would like to avoid throwing things in constructors as much as possible.
Is this good design to have a static class method that checks arguments the caller will give to the constructor. The documentation of the class will say, thou shall call this method to validate thine arguments before calling the constructor, or else segfault may befall thoust.
View 14 Replies
View Related
Oct 11, 2013
I have a COM Object created using ATL (without MFC Support)
The Object has 1 method that opens a Dialog (that does all the rest) and when finish - returns 2 values back (1 is enough)
Currently I call it from another EXE:
hr = CoCreateInstance(
CLSID_MyControl,
NULL,
CLSCTX_INPROC_SERVER,
IID_IMyControl,
(void**) &pMyControl
);
and then:
hr = pMyControl->MyMethod (ATL::CComBSTR(InputString1), ATL::CComBSTR(InputString2), &IntReturned, &IntReturned);
Is it possible to call it as is from a browser ?
How can I Instantiate the object and invoke my method (with params) from the browser ?
will it open the dialog ?
View 3 Replies
View Related
Feb 21, 2013
How do I call these functions from Mechanical.h???
Mechanical.h
#ifndef MECHANICAL_H_
#define MECHANICAL_H_
class statics { public:
template<class T> struct Inertia_types {
T Rec(T _x, T _y);
T Tri(T _x, T _y);
[Code] ...
I am trying to create templated functions which I can apply all data types to except for strings and other types such as wchar. Am I also writing these correctly? This is my first attempt ever doing this.
View 1 Replies
View Related
Mar 8, 2014
I have a class where a method based on the type passed I should return a value.
prototype declared in the header file:
template <typename T>int getNum() const;
Code of the cpp file:
template <typename T> int class::getNum() const{
int c = 0;
for(int i=0;i<v.size();i++)
if(typeid(*(Pro*)v.at(i)) == typeid(T)) c++;
return c;
}
To invoke the method as I do:
ostream & operator << (ostream & os, Pro & obj) {
return os << obj.getNum();
}
View 4 Replies
View Related
Oct 24, 2013
I have been experimenting with variadic templates with the aim of caching a call to a class method by storing away the object pointer, method pointer and parameters. I've actually had some reasonable success but have now hit a stumbling block. I now wish to wrap my parameters in a simple template class when I cache them. My success is as follows:
Using variadic template functions to store these pointers and paremeters;
I'm able to pass a method pointer and unwrapped parametersI'm able to pass wrapped parameters on their own.I'm NOT able to pass a method pointer and wrapped parameters I set up a little prototype project to demonstrate the issue and added comments above the function calls to indicate the compilation results. Here is the code:
Code:
#include "stdafx.h"
//////////////////////////////////////////////////
// Basic class with a simple method
//////////////////////////////////////////////////
class MyClass {
public:
char Method( int i, float f ) {
return 'A';
[code]....
But I'm convinced it should take three arguments, the method pointer and two wrapped parameters. Visual studio even suggested it should as shown below:
View 3 Replies
View Related
Dec 5, 2013
I'm trying to implement a simple template array class, but when i came into the operator< i actually have to use a template :
my code is something like :
template<typename _Type, std::size_t _Size>
class array {
public :
[Code] ......
but i am having an error of shadows template param 'class _Type' is it w/ the name conflict between the array template parameter and the function template parameter ?
View 6 Replies
View Related
Nov 6, 2013
Error1error C2955: 'DoubleLinkedListInterface' : use of class template requires template argument listdoublelinkedlist.h10
Error2error C2244: 'DoubleLinkedList<T>::DoubleLinkedList' : unable to match function definition to an existing declaration doublelinkedlist.cpp7
Error3 .cpperror C2244: 'DoubleLinkedList<T>::~DoubleLinkedList' : unable to match function definition to an existing declaration 12
.h
#pragma once
#include "DoubleLinkedListInterface.h"
#include "Node.h"
#include <iostream>
[Code]....
View 4 Replies
View Related
Jan 21, 2014
The compiler creates virtual table for the base class and also for the derived class whether we override it or not.
That means each class has separate virtual table. when we get the size of the each class with out any data members... the size of base is -- 4 bytes(64 bit) and the size of derived is -- 1
The size of base class 4 is correct since it creates the virtual pointer internally and its size is member data + virtual pointer, but it in this case I have included any data members so it has given 4 byts.
But why in case of derived is 1 byte, since it the derived class has overridden the virtual function from base, this will also contains the virtual pointer which will be pointing to derived class Vtable, it the size of the class suppose to be 4 instead of 1 byte.
#include<iostream>
class A{
public:
[Code].....
View 1 Replies
View Related