Is this an overide of a function?

I am messing around with this keyboard GUI and I am new to c++. I have never seen this before where two functions are being declared with the same name. What exactly is happening here?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  int KeyboardWidget::createBtn(QHBoxLayout* row, 
                              int          span, 
                              char         normal,
                              char         shift,
                              char         caps,
                              char         alt,
                              char         capAlt,
                              bool         autoRepeat)
{
  int id = m_allButtons.count();
  KeyboardWidgetButton* btn = new KeyboardWidgetButton(this, 
                                                       id,
                                                       c2s(normal),
                                                       c2s(shift),
                                                       c2s(caps),
                                                       c2s(alt),
                                                       c2s(capAlt));
  row->addWidget(btn, span);
  btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  btn->setAutoRepeat(autoRepeat);
  VERIFY(connect(btn,  SIGNAL(takeAction(int)),
                 this, SLOT(buttonClicked(int))));
  m_allButtons.append(btn);
  return id;
}


int KeyboardWidget::createBtn(QHBoxLayout*  row, 
                              int           span,
                              bool          checkable,
                              int           key, 
                              const char*   text,
                              bool          autoRepeat)
{
  int id = m_allButtons.count();
  KeyboardWidgetButton* btn = new KeyboardWidgetButton(this, id, key, QLatin1String(text));
  row->addWidget(btn, span);
  btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  btn->setCheckable(checkable);
  btn->setAutoRepeat(autoRepeat);
  VERIFY(connect(btn,  SIGNAL(takeAction(int)),
                      this, SLOT(buttonClicked(int))));
  m_allButtons.append(btn);
  return id;
}
Two functions in the same scope with the same name and different signature (either different numbers or types of parameters) are said to be overloads of each other.
the correct term is overLOADing.

what it does is give you two distinct functions with the same name that take different parameters. They could do completely different things, but this is generally used to provide the same functionality with alternative parameters.

consider, for example, two print number functions.

void printnum(int i);
void printnum(double d);

say they both have some form of
cout << parameter;

but you want to set precision and all that on the double and maybe write the integer in hex. So you make 2 versions that do these things, using different flags on the cout statement, but they both print a number, it just prints it the way you want it for the 2 types.

see https://www.learncpp.com/cpp-tutorial/76-function-overloading/




I am a bit confused. I know these methods are being called in one of two ways similar too:

snipit1)

createBtn(row1, 2, '1', '_', '1', '!', static_cast<char>(0xBC));

or snipit 2)

createBtn(row1, 3, false, Qt::Key_Backspace, "<---");

So it seems a bit odd to me because in the first deceleration of createBtn() the parameters are: QHBoxLayout*, int, char, char, char, char, char, and bool. However, in snipit1 and snipit2, the bool value never seems to be called.
Last edited on
I don't know QT well.
you may need to read the documentation.

there is another possibility, and that is a default parameter. that looks like this:

void derp(int x, int y = 3)
{
cout << x << " " << y << endl;
}


derp(5); // prints 5 3 -- y used the default
derp(3,4); //prints 3 4 -- y uses provided value, not the default

you will have to read your documentation to see if its 2 different functions for 2 different purposes or just a default parameter.

ironically, filling in a default parameter is 'overRIDE' as you initially said, my mistake :)
Last edited on
Look at the function declarations in the class definition. You'll probably find that the functions use default arguments for the last bool parameters.
Last edited on
However, in snipit1 and snipit2, the bool value never seems to be called.
Does createBtn() declare default parameters?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void f(const char *x, bool y = true){
    std::cout << "f (1): " << x << ", " << y << std::endl;
}

void f(int x, bool y = true){
    std::cout << "f (2): " << x << ", " << y << std::endl;
}

f("hello");
f("hello", true);
f("hello", false);
f(42);
f(42, true);
f(42, false);
I looked it up for you. the header is, according to the web,

cv::createButton (const String &bar_name, ButtonCallback on_change, void *userdata=0, int type=QT_PUSH_BUTTON, bool initial_button_state=false)

not sure why I can't get a hit on the btn spelling, but the hits I got were close enough to make me suspect the default param is what you are seeing, not an actual overload.
Last edited on
So the final bool value is just an optional value that defaults to true/false but can be specified? Either way the second createBtn() method is just an override right?

I am attempting to change the data type from static_cast<char> into ushort btw. If your interested I have an open topic:

http://www.cplusplus.com/forum/beginner/242158/

thanks guys!
Last edited on
looks like that is correct.


you have linked back to this topic.

a char is a type of int. a short can be assigned any char directly and that is fine, no need for all the casting (self documenting, but unnecessary). the signed type mixing can cause trouble, though...

Ill see if I see your topic, but generally you probably can't change these library functions directly, but you can make a wrapper function around them (with the same name, see the overloading bit again lol... ) and deal with it yourself to some extent.
I think OP probably meant to link to this: http://www.cplusplus.com/forum/beginner/242158/2/
Sorry yeah I posted the wrong link but I changed it thanks helios.
Topic archived. No new replies allowed.