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

Discussion in 'C++' started by Lord Labakudas, Sep 26, 2004.

  1. 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.
    Lord Labakudas, Sep 26, 2004
    #1
    1. Advertising

  2. Lord Labakudas

    Oliver S. Guest

    > 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.
    Oliver S., Sep 26, 2004
    #2
    1. Advertising

  3. "Lord Labakudas" <> wrote in message
    news:...
    > 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
    Dave Townsend, Sep 26, 2004
    #3
  4. Lord Labakudas

    JKop Guest

    Dave Townsend posted:

    >
    > "Lord Labakudas" <> wrote in message
    > news:...
    >> 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
    JKop, Sep 27, 2004
    #4
  5. Lord Labakudas

    Ron Natalie Guest

    "JKop" <> wrote in message news:5BR5d.32145$...
    > 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.
    Ron Natalie, Sep 27, 2004
    #5
  6. On 2004-09-26, Lord Labakudas <> 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
    Sergei Matusevich, Sep 27, 2004
    #6
  7. > 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
    Lord Labakudas, Sep 27, 2004
    #7
  8. Lord Labakudas

    David Hilsee Guest

    "Lord Labakudas" <> wrote in message
    news:...
    > > 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
    David Hilsee, Sep 28, 2004
    #8
  9. "Lord Labakudas" <> wrote in message
    news:...
    > > 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
    Dave Townsend, Sep 28, 2004
    #9
  10. Lord Labakudas

    JKop Guest

    Ron Natalie posted:

    >
    > "JKop" <> wrote in message
    > news:5BR5d.32145$...
    >> 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
    JKop, Sep 28, 2004
    #10
  11. Lord Labakudas

    Old Wolf Guest

    JKop <> wrote:
    > Ron Natalie posted:
    > > "JKop" <> 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
    Old Wolf, Sep 28, 2004
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,208
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,629
    Old Wolf
    Jan 20, 2004
  3. Oodini
    Replies:
    1
    Views:
    1,766
    Keith Thompson
    Sep 27, 2005
  4. arun
    Replies:
    8
    Views:
    451
    Dave Thompson
    Jul 31, 2006
  5. aling
    Replies:
    8
    Views:
    945
    Jim Langston
    Oct 20, 2005
Loading...

Share This Page