Is 'int' a class defined in C++ standard library ?

L

Lord Labakudas

Hi,

I have a very simple question. Is int a predefined class in the
standard C++ library. If yes, is it typedef'd as int somewhere ? I
wish to know this because, I wish to create a class that has all the
functionalities of int, but is also derived from another base class.
For example,

class Tuple: public Data, public int
{
Tuple()
{
;
}

~Tuple()
{
;
}
};

I am definitely missing something here because this gives a
compilation error. Is there are better way to do this ?

Thanks in Advance,
Vijay.
 
O

Oliver S.

I have a very simple question. Is int a predefined class in the
standard C++ library. If yes, is it typedef'd as int somewhere ?

No, it's a type "built-in" with every c++-compiler.
 
D

Dave Townsend

Lord Labakudas said:
Hi,

I have a very simple question. Is int a predefined class in the
standard C++ library. If yes, is it typedef'd as int somewhere ? I
wish to know this because, I wish to create a class that has all the
functionalities of int, but is also derived from another base class.
For example,

class Tuple: public Data, public int
{
Tuple()
{
;
}

~Tuple()
{
;
}
};

I am definitely missing something here because this gives a
compilation error. Is there are better way to do this ?

Thanks in Advance,
Vijay.

"int" is a C++ buit in type, not a class or struct, you can't inherit from
it. "int" doesn't have
operator member functions +, - etc, which you can override, etc these are
built into the language
and you can't mess with them. There is no typedef or header file for int.

What are you trying to do? If "Tuple" is a kind of tuple of integers, you
can emulate the
kind of int operations such as + - =, etc by overloading. Even if you
could inherit from
int, you would still have to reimplement each operator.

I'm guessing, but you could probably get what you want by having member
variables of
type int in each tuple ( I'm pressuming tuple is some fixed finite colletion
of integers used in
the mathematical sense)

Tuple Tuple::eek:perator+=( const Tuple& other)
{
_t1 += other._t1;
_t2 += other._t2;

// etc

return *this;

}


where _t1 , _t2 etc are int member variables ot the Tuple class.

Post more info if you want more help.

dave
 
J

JKop

Dave Townsend posted:
"int" is a C++ buit in type, not a class or struct, you can't inherit
from it. "int" doesn't have
operator member functions +, - etc, which you can override, etc these
are built into the language
and you can't mess with them. There is no typedef or header file for
int.

You can't inherit from the intrinsic types. If you really want to, you can
write a class that makes a clone of them and then do


class Blah : public IntrinsicClone<int>
{


I started one but never got around to finishing... I think I forgot why I
wanted to inherit from an intrinsic in the first place!


-JKop
 
R

Ron Natalie

JKop said:
You can't inherit from the intrinsic types. If you really want to, you can
write a class that makes a clone of them and then do

You can make them pretend to be the builtin types, but they will NEVER
act fully like them.
 
S

Sergei Matusevich

I have a very simple question. Is int a predefined class in the
standard C++ library. If yes, is it typedef'd as int somewhere ? I
wish to know this because, I wish to create a class that has all the
functionalities of int, but is also derived from another base class.
For example,

class Tuple: public Data, public int

Vijay,

As many others have pointed out, int is not a class and you can't
inherit from it.

Moreover, I feel very suspicious about the whole idea to have a class
"that has all the functionalities of int". Why do you need such thing,
and what exactly are these functionalities? Do you want your objects to
BEHAVE like int (i.e. to implement an "is-a" relationship), or do you
want to inherit and EXTEND the functionality of int (i.e. "reuse by
inheritance" paradigm)? In latter case, maybe you don't need
inheritance at all as simple aggregation will do the job; in former,
having correct casting to/from int will be enough - i.e. define
non-explicit constructor from int plus operator int().

Hope this helps!
 
L

Lord Labakudas

Post more info if you want more help.

Thanks for the useful information. Here is exactly what I am
attempting to do:

I am trying to develop a class of optimization algorithms that
consists of two parts:

1. An optimization engine that is common to all algorithms and acts on
(or takes as input) a generic data type (say, Data).
2. An objective function that is different for different algorithms,
and each act on specific data types (either an int, or a double, or a
vector, etc.,)

My plan to tackle this situation was to create a abstract base class
called Data on which the optimization engine acts on, and hence the
optimization engine is generic. The optimization engine itself is a
abstract base class (called OptimizationEngine) that contains the
following function

virtual double objfunc(const Data *)=0;

The actual optimization algorithms are classes derived from the
OptimizationEngine that overload the objective function class and then
typecast the Data pointer into whatever form they want. The candidates
for data include int, Vector, double, etc., (a mix of intrinsic and
defined classes). All the valid data types that can be used for the
obj func must be derived from Data. So, how to accomplish this without
using templates ?

Any ideas will be greatly appreciated.

Thanks,
Vijay
 
D

David Hilsee

Lord Labakudas said:
Thanks for the useful information. Here is exactly what I am
attempting to do:

I am trying to develop a class of optimization algorithms that
consists of two parts:

1. An optimization engine that is common to all algorithms and acts on
(or takes as input) a generic data type (say, Data).
2. An objective function that is different for different algorithms,
and each act on specific data types (either an int, or a double, or a
vector, etc.,)

My plan to tackle this situation was to create a abstract base class
called Data on which the optimization engine acts on, and hence the
optimization engine is generic. The optimization engine itself is a
abstract base class (called OptimizationEngine) that contains the
following function

virtual double objfunc(const Data *)=0;

The actual optimization algorithms are classes derived from the
OptimizationEngine that overload the objective function class and then
typecast the Data pointer into whatever form they want. The candidates
for data include int, Vector, double, etc., (a mix of intrinsic and
defined classes). All the valid data types that can be used for the
obj func must be derived from Data. So, how to accomplish this without
using templates ?

You used the word "generic" yet you don't want to use templates? Why?
Templates tend to be good for generic algorithms. A function that takes a
common base class tends to work well if the key operations for that class
are defined as virtual member functions of that class. It doesn't sound
like you have that situation, given that objfunc is the virtual member, and
AFAICT it is not a member of Data.
Any ideas will be greatly appreciated.

Well, I suppose you could write classes that derive from Data and contain
the types that you wish to inherit from (e.g. class Tuple { public: int
i; };), but I'd reexamine what you're doing first.
 
D

Dave Townsend

Lord Labakudas said:
Thanks for the useful information. Here is exactly what I am
attempting to do:

I am trying to develop a class of optimization algorithms that
consists of two parts:

1. An optimization engine that is common to all algorithms and acts on
(or takes as input) a generic data type (say, Data).
2. An objective function that is different for different algorithms,
and each act on specific data types (either an int, or a double, or a
vector, etc.,)

My plan to tackle this situation was to create a abstract base class
called Data on which the optimization engine acts on, and hence the
optimization engine is generic. The optimization engine itself is a
abstract base class (called OptimizationEngine) that contains the
following function

virtual double objfunc(const Data *)=0;

The actual optimization algorithms are classes derived from the
OptimizationEngine that overload the objective function class and then
typecast the Data pointer into whatever form they want. The candidates
for data include int, Vector, double, etc., (a mix of intrinsic and
defined classes). All the valid data types that can be used for the
obj func must be derived from Data. So, how to accomplish this without
using templates ?

Any ideas will be greatly appreciated.

Thanks,
Vijay


You might want to look at the Design Patterns book, in particular the
Strategy
pattern. This will make your design a bit more flexible, since you would be
able to vary both the optimization engine and the data class separately. As
an outline,

class NumberCruncher : public ......
{

public:
NumberCruncher( Engine* engine, Data* data );
double doNumberCrunching( )
{
return _engine->objfunc( _data ) ;
}

private:
Engine* _engine;
Data* _data;
};

.....
Engine* beanCounter = new BeanCounterEngine( 42 );
Data* beans = new BeanData();
NumberCruncher lazybeanCounter( beanCounter, beans );


Here Engine is an abstract class and engine would be some derived class from
Engine.
Similarly for Data and data. The advantage of this approach over
inheritance is that
if you have N Engines and M Data subclasses, you would have M*N different
possible subclasses
if you used in heritance, where as you only have 1 class in the above case.
Another advantage
is that Engine could be customized ( say for level of effort or accuracy of
results ) and then
plugged into the NumberCruncher class, thus giving you even more
flexibility.

Engine* beanCounter = new BeanCounterEngine( 42 );
Data* beans = new BeanData();
beanCounter->setAccuracy( 0.00001);
beanCounter->setLevelOfEffort( 1000000 );
NumberCruncher meanbeanCounter( beanCounter, beans );


You might even do all this with templates, this would remove the condition
that the Engine and
Data classes be related through inheritance, you could use any classes
provided they supported
the required interface ( like objfunc() ). If you look at Modern C++
Design by Andrei Alexandrescu
( I think that's how you spell his name), he writes about policy classes
which might work for
you in this situation

hope that helps.

dave
 
J

JKop

Ron Natalie posted:
You can make them pretend to be the builtin types, but they will NEVER
act fully like them.


How so? Define all the operators and define a "operator int()". What am I
missing?


-JKop
 
O

Old Wolf

JKop said:
Ron Natalie posted:

How so? Define all the operators and define a "operator int()". What am I
missing?

Here's one example:
int_wrapper i;
printf("%d\n", i); // runtime error
 

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,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top