simple template question

A

aaragon

Hi everyone,

I'm trying to create two simple template classes where one of them
depends on the other to initialize its member integer variable. After
some time, I decided to post a message here because I don't really
understand why I can't make this work.

template<int n>
class ClassA
{
public:
int n_;

ClassA() : n_(n)
{}
};

template<class ClassA>
class ClassB
{
public:
int m_;

ClassB() : m_(ClassA::n_)
{}
};

int main()
{
typedef ClassA<2> ca;
typedef ClassB<ca> cb;

cb cb1;
return 1;
}

The compiler errors that I get:

aaragon@aaragon-laptop:~/Desktop$ g++ test.cxx
test.cxx: In constructor 'ClassB<ClassA>::ClassB() [with ClassA =
ClassA<2>]':
test.cxx:28: instantiated from here
test.cxx:5: error: object missing in reference to 'ClassA<2>::n_'
test.cxx:17: error: from this location

I guess that the error lies on the fact that a typedef is not an
instantiation of the object so therefore I cannot call the variable n
within ClassA. On the other hand, I cannot instantiate ClassA to
create the type definition of classB because I receive a compiler
error that tells me that the instantiated object of ClassA cannot
appear in a constant expression.

Is there a way around this? Maybe the solution is real simple. Thank
you.
 
G

Gianni Mariani

aaragon said:
Hi everyone,

I'm trying to create two simple template classes where one of them
depends on the other to initialize its member integer variable. After
some time, I decided to post a message here because I don't really
understand why I can't make this work.

template<int n>
class ClassA
{
public:
int n_;

static const int n_ = n;

// the original definition is a member variable. What you want is a
"static". It also likely needs to be const. You can use an anonymous enum.
ClassA() : n_(n)
{}

Nuke the constructor.
....
 
R

Rolf Magnus

aaragon said:
Hi everyone,

I'm trying to create two simple template classes where one of them
depends on the other to initialize its member integer variable. After
some time, I decided to post a message here because I don't really
understand why I can't make this work.

template<int n>
class ClassA
{
public:
int n_;

ClassA() : n_(n)
{}
};

template<class ClassA>
class ClassB
{
public:
int m_;

ClassB() : m_(ClassA::n_)
{}
};

int main()
{
typedef ClassA<2> ca;
typedef ClassB<ca> cb;

cb cb1;
return 1;
}

The compiler errors that I get:

aaragon@aaragon-laptop:~/Desktop$ g++ test.cxx
test.cxx: In constructor 'ClassB<ClassA>::ClassB() [with ClassA =
ClassA<2>]':
test.cxx:28: instantiated from here
test.cxx:5: error: object missing in reference to 'ClassA<2>::n_'
test.cxx:17: error: from this location

I guess that the error lies on the fact that a typedef is not an
instantiation of the object so therefore I cannot call the variable n
within ClassA.

Right. You have a type, not an object, so there is no n_.
On the other hand, I cannot instantiate ClassA to create the type
definition of classB because I receive a compiler error that tells me that
the instantiated object of ClassA cannot appear in a constant expression.

Is there a way around this? Maybe the solution is real simple. Thank
you.

It depends on what you want to do. Maybe making n_ static is what you want.
 
A

aaragon

static const int n_ = n;

// the original definition is a member variable. What you want is a
"static". It also likely needs to be const. You can use an anonymous enum.




Nuke the constructor.
...

That worked!!!
However, I don't understand why, could you briefly explain me why is
working?
 
G

Gianni Mariani

aaragon said:
That worked!!!
However, I don't understand why, could you briefly explain me why is
working?

Learn about "static" - it's one of the most overused keywords int the
language and means different things at different contexts.

-------------------

static const int A = 1; // use 1

struct T
{
static const int A = 1; // use 2

void f( int c )
{
static const int A = c; // use 3
}
};
--------------------
use 1 : this use of static is "deprecated". Anonymous namespaces is the
moral equivalent. In this use of static, A is only visible to the
compilation unit.

use 2 : This use indicates that there is only 1 instance of member A for
all classes and is initialized in global scope. When an integer type
POD is declared const above it may be used in constant expresssions
(declare the size of an array or use in template argument e.g.) non POD
static members must be initialized out of the class definition.

use 3 : static in a function block indicates there is only one A and
that it is initialized the first time control passes through the function.

Your fav. C++ reference book will have this fully described with
examples if it's worth anything.
 
G

Gianni Mariani

Gianni said:
Learn about "static" - it's one of the most overused keywords int the
language and means different things at different contexts.

-------------------

static const int A = 1; // use 1

struct T
{
static const int A = 1; // use 2

void f( int c )
{
static const int A = c; // use 3
}
};
--------------------
use 1 : this use of static is "deprecated". Anonymous namespaces is the
moral equivalent. In this use of static, A is only visible to the
compilation unit.

use 2 : This use indicates that there is only 1 instance of member A for
all classes and is initialized in global scope. When an integer type

..... I meant to write :
use 2 : This use indicates that there is only 1 instance of member A for
all OBJECTS of this class ...
 
G

Gianni Mariani

aaragon said:
That worked!!!
However, I don't understand why, could you briefly explain me why is
working?

CORRECTED VERSION - cancelling previous version...

Learn about "static" - it's one of the most overused keywords int the
language and means different things at different contexts.

-------------------

static const int A = 1; // use 1

struct T
{
static const int A = 1; // use 2

void f( int c )
{
static const int A = c; // use 3
}
};
--------------------
use 1 : this use of static is "deprecated". Anonymous namespaces is the
moral equivalent. In this use of static, A is only visible to the
compilation unit.

use 2 : This use indicates that there is only 1 instance of member A for
all objects of this class and is initialized in global scope which makes
A in this use globally accessible. When an integer type POD is declared
const like above it may be used in constant expresssions (declare the
size of an array or use in template argument e.g.) non POD static
members must be initialized out of the class definition.

use 3 : static in a function block indicates there is only one A and
that it is initialized the first time control passes through the function.

Your fav. C++ reference book will have this fully described with
examples if it's worth anything.
 
A

aaragon

Learn about "static" - it's one of the most overused keywords int the
language and means different things at different contexts.

-------------------

static const int A = 1; // use 1

struct T
{
static const int A = 1; // use 2

void f( int c )
{
static const int A = c; // use 3
}};

--------------------
use 1 : this use of static is "deprecated". Anonymous namespaces is the
moral equivalent. In this use of static, A is only visible to the
compilation unit.

use 2 : This use indicates that there is only 1 instance of member A for
all classes and is initialized in global scope. When an integer type
POD is declared const above it may be used in constant expresssions
(declare the size of an array or use in template argument e.g.) non POD
static members must be initialized out of the class definition.

use 3 : static in a function block indicates there is only one A and
that it is initialized the first time control passes through the function.

Your fav. C++ reference book will have this fully described with
examples if it's worth anything.

Thank you...
 
G

Gianni Mariani

....

CORRECTED VERSION - cancelling previous version...
....

It appears that after all these years, there are still bugs in
cancelling messages ! I don't think I have ever seen it work. We'll
try again in another 5 years.
 
J

James Kanze

It appears that after all these years, there are still bugs in
cancelling messages ! I don't think I have ever seen it work. We'll
try again in another 5 years.

Because of the large number of forged cancels, I believe that
most sites today simply ignore them. (They worked, for me,
fifteen years ago. But not now.)
 
G

Gianni Mariani

James said:
Because of the large number of forged cancels, I believe that
most sites today simply ignore them. (They worked, for me,
fifteen years ago. But not now.)

I don't think it ever worked for me - can't they use a PKI type auth ?
 
J

James Kanze

James Kanze wrote:

[...]
I don't think it ever worked for me - can't they use a PKI type auth ?

[This is getting awfully off topic...]

In theory, there are any number of solutions which could be
used. In practice, the mass of systems out there is now too
large to start playing with the protocol; add any formal
authentication, and it will be years before all, or even most,
of the systems will support it.

Anyway, I did successfully cancel some messages I'd posted back
in the early 1990's.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top