473,396 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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.
Jul 22 '05 #1
10 1460
> 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.
Jul 22 '05 #2

"Lord Labakudas" <ll********@yahoo.com> wrote in message
news:d6**************************@posting.google.c om...
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::operator+=( 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



Jul 22 '05 #3
Dave Townsend posted:

"Lord Labakudas" <ll********@yahoo.com> wrote in message
news:d6**************************@posting.google.c om...
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.


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
Jul 22 '05 #4

"JKop" <NU**@NULL.NULL> wrote in message news:5B*******************@news.indigo.ie...
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.

Jul 22 '05 #5
On 2004-09-26, Lord Labakudas <ll********@yahoo.com> wrote:
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!

--
Sergei Matusevich,
Brainbench MVP for C++
http://www.brainbench.com

Jul 22 '05 #6
> 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
Jul 22 '05 #7
"Lord Labakudas" <ll********@yahoo.com> wrote in message
news:d6**************************@posting.google.c om...
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 ?


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.

--
David Hilsee
Jul 22 '05 #8

"Lord Labakudas" <ll********@yahoo.com> wrote in message
news:d6**************************@posting.google.c om...
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

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


Jul 22 '05 #9
Ron Natalie posted:

"JKop" <NU**@NULL.NULL> wrote in message
news:5B*******************@news.indigo.ie...
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.

How so? Define all the operators and define a "operator int()". What am I
missing?
-JKop
Jul 22 '05 #10
JKop <NU**@NULL.NULL> wrote:
Ron Natalie posted:
"JKop" <NU**@NULL.NULL> wrote:

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.


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
Jul 22 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ben | last post by:
Hi. I am currently trying to move compilers (VC6 -> VC7 in fact) and I'm having a few problems (so far exclusively to do with better standard conformance). Anyhow, whilst rebuilding a...
12
by: Woodster | last post by:
I currently have some code for an application that is running on Win32. I have tried to keep anything not directly gui related as separate as possible for portability reasons, including file...
44
by: JKop | last post by:
You know how the saying goes that *unsigned* overflow is... well.. defined. That means that if you add 1 to its maximum value, then you know exactly what value it will have afterward on all...
2
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking afterwards with ILDASM at what is visible in those assemblies from a...
4
by: Richard | last post by:
First question - let's get this out of the way since it might be the solution to all my woes: Does it make sense to have a .cpp file for a class that is declared as having pure virtual functions...
10
by: Old Wolf | last post by:
Consider the following program: #include <stdio.h> int main(void) { /* using malloc to eliminate alignment worries */ unsigned long *p = malloc( sizeof *p ); if ( p && sizeof(long) ==...
18
by: mati | last post by:
Hi The following code works: #include <vector> class C { private: static const int m_static = 2; public: void f(const std::vector<int>& v)
8
by: Ole Nielsby | last post by:
I want to create (with new) and delete a forward declared class. (I'll call them Zorgs here - the real-life Zorks are platform-dependent objects (mutexes, timestamps etc.) used by a...
4
by: James Kanze | last post by:
On Aug 13, 5:32 am, Jack Klein <jackkl...@spamcop.netwrote: While you've made a number of important points: In other words, according to the C standard, any signal
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.