-1

I have some code like:

#include "Communicate.h"

Communicate::Communicate(const wxString& title)
       : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(290, 150))
{
  m_parent = new wxPanel(this, wxID_ANY);

  wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);

  m_lp = new LeftPanel(m_parent);
  m_rp = new RightPanel(m_parent);

  hbox->Add(m_lp, 1, wxEXPAND | wxALL, 5);
  hbox->Add(m_rp, 1, wxEXPAND | wxALL, 5);

  m_parent->SetSizer(hbox);

  this->Centre();
}

from this tutorial: http://zetcode.com/gui/wxwidgets/ - First Apps

What does operator | mean:

hbox->Add(m_lp, 1, wxEXPAND | wxALL, 5);
hbox->Add(m_rp, 1, wxEXPAND | wxALL, 5);
Philip Pittle
  • 11,821
  • 8
  • 59
  • 123
  • 1
    It's bitwise OR, nothing specific for wxwidgets or UI programming. – πάντα ῥεῖ Aug 04 '14 at 11:16
  • 1
    A beginner C tutorial would cover this. Please read. – david.pfx Aug 04 '14 at 11:42
  • 1
    This question appears to be off-topic because it is a request to teach core language features. – david.pfx Aug 04 '14 at 11:44
  • I don't ask about C. I ask about C++. I generally use || as OR but I haven't seen | operator before so I just ask. If it is sth 'inherited' from C, please just write it because tutorials using "clear" C are generally useless 4 me and I don't want to look into it only to know about simple | operator. – Tomasz Hamerla Aug 04 '14 at 12:01

1 Answers1

7

| is a bitwise OR.

Libraries generally define different masks, such as your wxEXPAND and wxALL which are usually integer constants with only one bit set.

When you use the bitwise-or operator, you can combine these to create what's called a bitfield, an integer with bits that you define set.

You combine these like this:

wxEXPAND | wxALL

which will create a bitfield with the bits from wxEXPAND and wxALL set.

Usually the library will then check if these bits are set like this for example:

if (bitfield & wxEXPAND) { .. // wxEXPAND is set

This is a bitwise AND. The test will return true if and only if the wxEXPAND bit is set in bitfield.

Libraries use this to allow you to pass multiple options in a single register, for example.

Matt Phillips
  • 9,465
  • 8
  • 44
  • 75
David Xu
  • 5,555
  • 3
  • 28
  • 50