473,804 Members | 3,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1502
> 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********@yah oo.com> wrote in message
news:d6******** *************** ***@posting.goo gle.com...
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********@yah oo.com> wrote in message
news:d6******** *************** ***@posting.goo gle.com...
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******** ***********@new s.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********@yah oo.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 OptimizationEng ine) that contains the
following function

virtual double objfunc(const Data *)=0;

The actual optimization algorithms are classes derived from the
OptimizationEng ine 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********@yah oo.com> wrote in message
news:d6******** *************** ***@posting.goo gle.com...
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 OptimizationEng ine) that contains the
following function

virtual double objfunc(const Data *)=0;

The actual optimization algorithms are classes derived from the
OptimizationEng ine 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********@yah oo.com> wrote in message
news:d6******** *************** ***@posting.goo gle.com...
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 OptimizationEng ine) that contains the
following function

virtual double objfunc(const Data *)=0;

The actual optimization algorithms are classes derived from the
OptimizationEng ine 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 doNumberCrunchi ng( )
{
return _engine->objfunc( _data ) ;
}

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

.....
Engine* beanCounter = new BeanCounterEngi ne( 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 BeanCounterEngi ne( 42 );
Data* beans = new BeanData();
beanCounter->setAccuracy( 0.00001);
beanCounter->setLevelOfEffo rt( 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******** ***********@new s.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

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

Similar topics

2
3714
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 third-party library (let's call it library B) I'm getting some unresolved externals when it tries to use another library (library A) that includes code like this in a header:
12
2604
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 access. Now has come the time to try and implement the program on a Windows CE platform. Problem is, that the MFC CArchive class uses an integer file pointer and calls to open, close etc and my file handling class is doing everything with FILE *, such...
44
8580
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 implementations. But then you have signed integers. Let's say a signed integer is set to its maximum positive value. If you add 1 to it, what happens?: A) It's implementation defined what value it will
2
2061
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 managed point-of-view I've noticed that: 1) for each managed and unmanaged C function (not C++ classes) I get a public managed static method (defined on a 'Global Functions' class) in the generated assembly with an export name of the form
4
2901
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 in its .h file? Here's my predicament. I'll put this in general terms since it's happening across several classes. I have a base class, let's say it's named A. In A.h I DECLARE several pure virtual ( = 0) functions, a virtual destructor, and...
10
2004
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) == sizeof(int) )
18
5659
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
2146
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 cross-platform scripting engine. When the scripting engine is embedded in an application, a platform-specific support library is linked in.) My first attempt goes here: ---code begin (library)---
4
2997
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
9707
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10586
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10082
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7622
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6856
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5658
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3823
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2997
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.