Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Overriding Functions with variable argument list.

Overriding Functions with variable argument list.

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Suyash
    wrote on last edited by
    #1

    I had a understanding that overriding the the function with variable argument list will result in to ambiguity. However when i tried to compile and run following code on VC++ 6.0, its working as expected : ///////////////////////////////////// void Func1( int i, ... ) //Lets refer it as Func1VariableArg { // do something. } void Func1( int i, int j) //Lets refer it as Func1FixedArg { // Do something. } class A { }; int main(int argc, char* argv[]) { Func1(1, 1); // Func1FixedArg is called as expected. Func1(1, "1"); // Func1VariableArg is called as expected. A a; Func1(1, a); // Func1VariableArg is called as expected. // This is the spoil spot Func1(1, 1.0)// here we would expect Func1VariableArg to //be called as second argument is double, however compiler // implicitely typecasts it to int and calls the Func1FixedArg. // however a warning for the same is issued at complie time. return 0; } ///////////////////////////////////// Some one has any comments about this practice. Do we have any issues in this practice ?, any comments from C++ language specifications on this. Thanks - Suyash

    T 1 Reply Last reply
    0
    • S Suyash

      I had a understanding that overriding the the function with variable argument list will result in to ambiguity. However when i tried to compile and run following code on VC++ 6.0, its working as expected : ///////////////////////////////////// void Func1( int i, ... ) //Lets refer it as Func1VariableArg { // do something. } void Func1( int i, int j) //Lets refer it as Func1FixedArg { // Do something. } class A { }; int main(int argc, char* argv[]) { Func1(1, 1); // Func1FixedArg is called as expected. Func1(1, "1"); // Func1VariableArg is called as expected. A a; Func1(1, a); // Func1VariableArg is called as expected. // This is the spoil spot Func1(1, 1.0)// here we would expect Func1VariableArg to //be called as second argument is double, however compiler // implicitely typecasts it to int and calls the Func1FixedArg. // however a warning for the same is issued at complie time. return 0; } ///////////////////////////////////// Some one has any comments about this practice. Do we have any issues in this practice ?, any comments from C++ language specifications on this. Thanks - Suyash

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      if the compiler can find a function which signature fits exactly to the argument types it receives, then there's no problem. the ambiguity can arrise when the compiler has to do some implicit conversions... in you case Func1(1, 1); results in calling void Func1(int, int) because literal numbers are firstly interpreted as ints. i don't know the priorities of the compiler's implicit casts, but take this for instance : Func1(1, 'a'); 'a' is of type char, but no function Func1(int, char) exists, so it continues looking if this call fits another existing overload before casting some parameters. only if the compiler makes some implicit casts an ambiguity can come, because the call can then fit several overloads...

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups