class global function scope override

Discussion in 'C++' started by g3rc4n@gmail.com, Apr 12, 2009.

  1. Guest

    i recently realized you could override global function scope like this

    int add(int,int);
    double add(double,double);

    void f(double a, double b){
    int add(int,int); // hides double add(double,double)
    double c = add(a,b);
    }

    but i want to know if you could use it in a struct like this(doesn't
    work)

    struct foo{
    using int add(int,int);
    }

    becuase i'm sure i could use this feature for something interesting
    for metaprogramming like this

    template<typename T>
    struct impl{
    using T add(T,T);
    };

    template<class T>
    struct addition : private impl<T>{
    template<class U>
    static U add(U a, U b){
    return static_cast<U>(add(a,b)); // call T add(T,T)
    }
    };


    thanks in advance
    , Apr 12, 2009
    #1
    1. Advertising

  2. Guest

    right realized i have to call the static function something other than
    add (add_), i'm aware i could put

    T add(T,T);

    before something reminds me
    , Apr 12, 2009
    #2
    1. Advertising

  3. SG Guest

    On 12 Apr., 19:17, "" <> wrote:
    > i recently realized you could override global function scope like this
    >
    > int add(int,int);
    > double add(double,double);
    >
    > void f(double a, double b){
    >   int add(int,int); // hides double add(double,double)
    >   double c = add(a,b);
    >
    > }
    >
    > but i want to know if you could use it in a struct like this(doesn't
    > work)
    >
    > struct foo{
    >   using int add(int,int);
    >
    > }


    No, you can't.

    > becuase i'm sure i could use this feature for something interesting
    > for metaprogramming like this
    >
    > template<typename T>
    > struct impl{
    >   using T add(T,T);
    >
    > };


    What's the intention? What do you expect to happen?

    Be sure to be familiar with
    http://www.gotw.ca/publications/mill08.htm

    Cheers!
    SG
    SG, Apr 12, 2009
    #3
  4. SG Guest

    On 12 Apr., 19:17, "" <> wrote:
    > i recently realized you could override global function scope like this
    >
    > int add(int,int);
    > double add(double,double);
    >
    > void f(double a, double b){
    >   int add(int,int); // hides double add(double,double)
    >   double c = add(a,b);
    >
    > }


    Oh, I forgot to mention: "overriding" != "hiding"
    The only functions you can override are virtual functions.

    Cheers!
    SG
    SG, Apr 12, 2009
    #4
  5. Guest

    On Apr 12, 6:36 pm, SG <> wrote:
    > On 12 Apr., 19:17, "" <> wrote:
    >
    > > i recently realized you could override global function scope like this

    >
    > > int add(int,int);
    > > double add(double,double);

    >
    > > void f(double a, double b){
    > >   int add(int,int); // hides double add(double,double)
    > >   double c = add(a,b);

    >
    > > }

    >
    > Oh, I forgot to mention: "overriding" != "hiding"
    > The only functions you can override are virtual functions.
    >
    > Cheers!
    > SG


    what, no i'm changing the scope of global functions, thanks for the
    link btw

    my intentions are just for fun, no real use
    , Apr 12, 2009
    #5
    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. Paul Opal
    Replies:
    12
    Views:
    926
    Paul Opal
    Oct 11, 2004
  2. Replies:
    2
    Views:
    892
    Bengt Richter
    Aug 1, 2005
  3. Steven T. Hatton
    Replies:
    9
    Views:
    458
  4. Ed
    Replies:
    1
    Views:
    518
  5. Andrew Falanga
    Replies:
    2
    Views:
    191
    Andrew Falanga
    Nov 22, 2008
Loading...

Share This Page