C++ :: Inheriting One Variadic Class From Another?

Jan 30, 2014

I have a variadic base class with a pure-virtual function per type:

Code:
template <typename ... Types>
class Base;
template <typename T, typename ... Types>
class Base<T,Types...>: public Base<Types...>
{
public:
using Base<Types...>::doSomething;

[Code] ......

Now, I'd like to inherit from it using another variadic class, which provides implementations of doSomething(), but I run into trouble --- where do I indicate it derives from Base?

Code:
template <typename ... Types>
class Derived;
template <typename T, typename ... Types>
class Derived<T,Types...>: public Derived<Types...>

[code]....

I see two possible approaches:

First, virtual inheritance *might* get me what I want, but I don't know how bad a performance hit that might be.

Second, I could do some magic where the full set of types is captured in a tuple at the lowest level and continually passed up, then re-expanded in the base case to indicate Base inheritance. However, I'm not sure if that can be done in an unambigious manner; I can't have two variadic packs at once (Types... and the tuple contents), and I'm not sure if there's a way to use enable_if to check if an arbitrary template type is any kind of tuple.

View 2 Replies


ADVERTISEMENT

Visual C++ :: Syntax In Class Definition While Inheriting Another Class

Apr 21, 2014

#include "IMyIntData.h"
class MyIntData : public CPMUnknown<IMyIntData>
{

I need to know what this syntax means (including MyIntData in angular brackets after parent class name) where IMyIntData is the Interface from where MyIntData is derived.

View 1 Replies View Related

C++ ::  Template Class Inheriting A Templated Class

Dec 9, 2014

I'm making a minimal spanning tree class that inherits a graph class, both are templated. This is my mstree.h so far:

#ifndef _MSTREE_H_
#define _MSTREE_H_
#include "graph.h"
namespace MSTreeNameSpace

[Code]...

and I keep getting these errors:

mst.h:9:25: error: expected template-name before ‘<’ token
class MST : public Graph<T>
^
mst.h:9:25: error: expected ‘{’ before ‘<’ token
mst.h:9:25: error: expected unqualified-id before ‘<’ token

View 3 Replies View Related

C++ :: Struct Inheriting From A Class Or A Class Inherit From A Struct?

Mar 9, 2012

I just read and have known for a while that classes are private (members and inheritance) by default and structs are public. But my question then comes what if.. a struct inheriting from a class or a class inheriting from a struct?

View 3 Replies View Related

C :: Variadic Macro Function

Sep 12, 2013

I'm doing right now is creating a function that callocs (I prefer this to malloc) and returns a string, and it will work similar to printf, I'm calling the function alloCpy(),I have several values that I need in a malloced string, so I call Code: myAllocedString = alloCpy("Value 1 is %s, value 2 is %s, and value 3 is %d", str1, str2, num); To do this I'm using the Variadic Macro, the reason I'm not just using a Variadic Function such as this: Code: char* alloCpy(char *format, ...) {} is because I need to append NULL to the end for the sake of looping through arguments, and I'm understanding it thusfar, but I have a few issues, first of all, I tried defining the Macro in a header file, but when I try to call it I get the error "Undefined reference to alloCpy". Also, to loop through arguments to get string lengths I'm using va_arg(args, char*) which requires all the arguments to be of type char*. Here is my code:
myheader.h:

Code:

#define alloCpy(format, ...) _alloCpy(format, ##__VA_ARGS__, NULL);
char* _alloCpy(char *format, ...); mycfile.c: Code: char* _alloCpy(char *format, ...) {
va_list args;
va_start(args, format);
int args_len = 0;
}

[code]....

So, how can I do this to, first of all, make my macro function accessible from other files importing myheader.h, and second, how can I make it accept any type of argument like printf, so that my example above would work?

View 3 Replies View Related

C++ :: Variadic Template Basic Syntax

Jul 31, 2014

I had to learn how to use variadic templates recently, and had trouble finding simple examples that just showed the basic syntax.

So I decided to write one myself. Admittedly, it's a bit on the long side, but that is mostly because it includes five specializations.

insert Code:
// Variadic.C
// Compile command: g++ Variadic.C -std=c++0x
// I used GCC version 4.6.3 on Ubuntu.

// This file contains a basic variadic template with five specializations.
// It is intended for non-software engineers who are looking for a simple
// example of variadic template syntax.

[Code] ....

View 3 Replies View Related

C++ :: How To Remove Last Argument In Variadic Template

Nov 1, 2013

I wonder if it is possible to remove the last argument in an argument pack? Below is an example on what I want to accomplish:

template<template<int...> class A,int... Ints>
A<remove_last_int<Ints...>::list> func(const A<Ints...> & a0)
{
A<remove_last_int<Ints...>::list> a;
...
//Here a set the members of a based on a0.
...
return a;
}

For example, I want the return a A<1,2> value from (const A<1,2,3> & a0)

View 8 Replies View Related

C++ :: Enable If With Variadic Template Arguments

Sep 17, 2014

template <typename FIRST, typename... REST, typename std::enable_if<std::is_convertible<FIRST, Base*>::value>::type* = nullptr>
void foo (FIRST first, REST... rest) {}

that successfully allows me to enable the function foo() only if FIRST is convertible to Base*, but I also only want foo() enabled if each type in REST... meets the same condition. What is the syntax for that? If no such syntax exists, how to achieve that effect?

View 7 Replies View Related

C++ :: Variadic Templates And Type Composition?

Jan 28, 2014

Let's say I have a variadic type A and a non-variadic type B:

Code:
template <typename ... Args>
class A
{};
template <typename T>
class B
{};

Now, I'd like to do the following (pseudocode since I don't know how to do this yet):

Code:
template <typename ... Args>
class C: public A<B<Args1>,B<Args2>,B<Args3>....>
{};

Essentially, I'd like to take the parameter pack used for C and compose B around each element individually, then pass the result to A.

how I can do this?

View 2 Replies View Related

C++ :: Variadic Templates And Overload Resolution

Jan 2, 2014

I'm trying to learn how to use variadic templates, and I decided a great example would be serializing a series of types into a stringstream:

Code:
// Send a fully constructed message.
virtual void send(ostringstream &msg) = 0;
// Construct a message from the arguments and send it.
// This is the usual entry point.
template <typename ...Args>
void send(Args ...args {

[Code] ....

This works fine, so far as I can tell. However, I decided to see if I could specialize the way certain types are serialized. I tried using a Google Protocol Buffer object as an example, and added this:

Code:
// Handle a protocol buffer type while constructing a message.
template <typename ...Args>
void send(ostringstream &msg,
const google::protobuf::MessageLite &protobuf, Args ...args) {
std::string msg_str = protobuf.SerializeAsString();
msg << msg_str;
send(msg,args...);
}

I would expect this overload to be preferred over the generic T overload when a protobuf object (which always inherits from MessageLite) is passed into send() anywhere in the list. However, this is not happening. I am getting an error message to the effect that << doesn't know how to deal with my concrete type, pointing at the T overload.

View 4 Replies View Related

C++ :: Passing Variadic References To Template Function?

Dec 14, 2014

I'm having some problems in understanding how the code below works and why it produces the output it produces.. What I'd expect is that both functions, namely `add_1' and `add_2', would print the same output; but I've been proven wrong :/ So why does the second one get different memory addresses for the same variable?

Code should be self-explaining:

Code: template<typename... Types>
void add_1(Types&&... values)
{
// by the way: why do i have to use `const int' instead of `int'?
std::vector<std::reference_wrapper<const int>> vector{
std::forward<Types>(values)...};
std::cout << "add_1:" << std::endl;
for (const auto& value:vector) {
std::cout << &value.get() << std::endl;

[code].....

View 4 Replies View Related

C++ :: Variadic Function - Parameters Get Passed In Reverse

Nov 17, 2014

I noticed that when using variadic functions, if I pass the va_arg() as parameter to a function, the parameters get passed in reverse. Is that expected?

For example, the following code outputs
Code:
1 2
2 1

Code:
#include <iostream>
#include <stdarg.h>

void foo_func(int v1, int v2)
{
std::cout << v1 << " " << v2 << std::endl;

[Code] .....

View 3 Replies View Related

C++ :: Expanding Variadic Template Arguments In Lambda

Feb 1, 2013

I'm trying to expand a template + argument parameter list inside a lambda function like this:

template <typename Class, typename ...Args>
static void create(Args ...args) {
// Perform pre-thread creation work.
std::thread([=]()

[Code] ....

But this does not work:

The compiler error is "error: parameter packs not expanded with ‘...’:|"

However, when I do the following:

template <typename Class, typename ...Args>
static void create(Args ...args) {
// Pre-thread work.
auto tthr = [](Args ...ar) -> void {

[Code] ....

It works just fine. That shows that lambda threads are able to take variadic arguments...

So here is my question; what is the correct capture clause for capturing the variadic object correctly?

View 3 Replies View Related

C++ :: Ambiguous Call On Overloaded Variadic Template Operators

Oct 30, 2013

When compiling the code

#include "tensor.h"
int main() {
Tensor<2,-2> m = {{1,2},{1,3}};
Tensor<2> v = {1,5};
std::cout<<m*v<<"

[Code] ....

Why do I get an ambiguity and why is not the wanted operator*-overload (the last one in the tensor.h file) not even mentioned as one of the candidates? Is it clear what I want to do? And if so, what can I do to make the call unambiguous?

View 3 Replies View Related

C++ :: Variadic Template Function Parameters And Method Pointers?

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

C++ :: Variadic Templates - Enter Unlimited Amount Of Types In Function

Feb 25, 2013

I'm looking for a way to enter an unlimited amount of types in the <> part of a template function, I found Variadic templates but I'm not sure if it can do it, all the examples I've found are similar to the C argument list and don't use the <> part of the template at all.

I tried this, but the overload is ambiguous?

#include <iostream>
#include <typeinfo>
template<typename T>
void stuff() {
std::cout << typeid(T).name() << "

[Code] ....

View 2 Replies View Related

C++ :: Instance Of Class A Vs Class B Derived From Class A

Jul 3, 2014

There are two ways to access the members of class A inside class B:

1) Making an instance of class A in class B
2) Deriving class B from class A

So what is the basic difference in both ways as we can do same kind of work with both ways?

View 1 Replies View Related

C++ :: How To Get Object Of Original Class From Function Of Other Class Where Other Class Object Is Member Of Original Class

Jan 21, 2013

The case is like

class B{
public:
somedata;
somefunction();
}
class A{
public:
data;
function();
}

in somefunction i want a pointer to current object of class A m new to c++

View 2 Replies View Related

C++ :: Calling Derived Class Functions In A Function With Parameter Of Base Class

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

C++ :: Size Of Derived Class With Overriding Virtual Functions From Base Class?

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

C++ :: Transfer Values Set In Privates Of Base Class By Object Of One Derived Class To Another

Apr 26, 2014

I have my main.cpp like this:

#include <iostream>
#include "curve1.h"
#include "curve2.h"
using namespace std;
int main() {
Curve1 curve1Obj;
Curve2 curve2Obj;

[Code]...

Base class Score has two derived classes Curve1 and Curve2. There are two curve() functions, one is in Curve1 and other in Curve2 classes. getSize() returns the value of iSize.

My base class header score.h looks like this:

#ifndef SCORE_H
#define SCORE_H
class Score {
private:
int *ipScore;
float fAverage;
int iSize;

[Code]...

You can see that I have used curve1Obj to enter scores, calculate average and output. So if I call getSize() function with cuve1Obj, it gives the right size that I took from user in enterScores() function. Also the result is same if I call getSize() in score.cpp definition file in any of the functions (obviously).
.....

The problem is when I call curve() function of Curve2 class in main (line 23) with the object curve2Obj, it creates a new set of ipScore, fAverage and iSize (i think?) with garbage values. So when I call getSize() in curve() definition in curve2.cpp, it outputs the garbage. .....

How can I cause it to return the old values that are set in curve1.cpp?

Here is my curve2.cpp

#include <iostream>
#include "curve2.h"
using namespace std;
void Curve2::curve() {
cout << "getSize() returns: " << getSize() << endl; // out comes the garbage
}

Can I use a function to simply put values from old to new variables? If yes then how?

View 3 Replies View Related

C++ :: How To Initialize Static Member Of Class With Template And Type Of Nested Class

Oct 7, 2014

How to initialize a static member of a class with template, which type is related to a nested class?

This code works (without nested class):

#include<iostream>
using namespace std;
struct B{
B(){cout<<"here"<<endl;}
};
template<typename Z>

[Code] ,....

View 1 Replies View Related

Visual C++ :: Reach Top Class Inherits From Goal Class - Linker Error

Dec 10, 2012

Linker error.

First off the error

Code:
Error1error LNK2019: unresolved external symbol "public: __thiscall ReachTop<class Character>::ReachTop<class Character>(class Character *)" (??0?$ReachTop@VCharacter@@@@QAE@PAVCharacter@@@Z) referenced in function "void __cdecl `dynamic initializer for 'gReachTop''(void)" (??__EgReachTop@@YAXXZ)Main.objDecisionTest

Reach Top class inherits from Goal Class

Goal Class

Code:
#ifndef _GOAL_H
#define _GOAL_H
#include "Action.h"
#include <list>
template <class T>
class Goal

[Code] ....

Code to create

Code:
Character* gCharacter = new Character(1, gWorld);
Goal<Character>* gReachTop = new ReachTop<Character>(gCharacter);

I can provide the character class and its inheritance aswell if you like.

View 4 Replies View Related

C++ :: Deleting Array Of Derived Class Objects Through Base Class Pointer

Mar 21, 2015

In this book, item 3 is about never treat arrays polymorphically. In the latter part of this item, the author talks about the result of deleting an array of derived class objects through a base class pointer is undefined. What does it mean? I have an example here,

Code:
class B
{
public:
B():_y(1){}
virtual ~B() {
cout<<"~B()"<<endl;

[Code] ....

This sample code does exactly what I want. So does the author mean the way I did is undefined?

View 1 Replies View Related

C++ :: Creating Array Of Pointers To Base Class To Point To Derived Class Objects Dynamically

Jan 16, 2013

Please consider the following code :

#include <iostream>
using namespace std;
class superclass;
class subclass1;
class subclass2;

[Code] ....

As you can see I want to create a dynamically allocated storage of references to a parent class each of which can then point to a child class, how ever I do not know how to extract the child class out again from that array so i may access its variable b.

View 2 Replies View Related

C++ :: Initializing Inner-objects Of Base Class From Driven-class Constructor

Jan 6, 2015

Let's say I have a Car object , and it contains inner Engine object.

Code:
struct Car{
Engine mEngine;
};

In order to initialize the engine object NOT by the default constructor (if it has any) , we use initialization semantics:

Code:
Car::Car:
mEngin(arg1,arg2,...)
{
other stuff here
}

Now it gets tricky: Let's say a Car objects has 10 inner objects, each object has about 5 variables in it . Car is a base class for , e.g. , Toyota class. you don't want the Car class to have a constructor with 50 arguments. Can the inner objects of Car be initialized from the base class , e.g. Toyota?

Code:
class Toyota:
Car(...),
mEngine(...),
mGear(..)
{
...
};

The other options are:
1) like said , create a Car constructor which gets 50 arguments, then initialize Car as whole from Toyota - the code becomes less readable and less intuitive
2) Car constructor which get built-objects as arguments and initialize the inner objects with copy constructor . the code gets more readable but then you create many excess objects .

View 5 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved