if statements in stucts/classes?

is it possible to use if statements within a struct?
In a C struct, no.

In a C++ struct, which can have member functions, yes.

What are you trying to do? Tell us in detail so we can advise you.

And if you have any source code do post it, with code tags.
@George_P just making a text box, and need to detect input
Just making a text box, and need to detect input

https://docs.microsoft.com/en-us/windows/win32/uxguide/ctrl-text-boxes
*grabs a pair of pliers and starts pulling teeth*

"a text box" you say. That is either really easy, or nigh um-possible, depending on the OS and how it is programmed. GUI? Console-mode?

Console mode C++ without 3rd party libraries it would be a lot of work. Lots and lots of work.

This is starting to look like an X-Y problem.
If you had just replied, "Yes", you would have forced OP to think of a more thoughtful question. :)
Last edited on
Gee, if only someone other than myself had something substantive to say, instead of offering lame advice. *sigh*
is it possible to use if statements within a struct?


Why - what are you trying to achieve?

just making a text box, and need to detect input


For which os? For console or gui? What is the 'text box' supposed to do?

Much more info is really needed before further info can be given.

As George has already said, no in c, yes in C++. But that the answer is really pretty meaningless without context.

As George has already said, no in c, yes in C++.

I'd say that the problem is same in both.

If you read Meyers or Sutter, they suggest non-friend non-member functions as part of the interface of a class. The C has only non-friend non-member functions ...

The crucial point is functions. A struct/class defines a type. The functions contain code that is executed. The if statement has to be evaluated, executed (as part of function).

A "text box" is just data. By itself it does nothing. It can/should have associated functions (member or not) that someone calls at some point.


Let say a program has a "button". That "variable" has some data that is used to draw on screen (with some functions) what looks like a button. When user clicks mouse, window system registers location of mouse and determines that it was on application and sends the application the "click event" -- calls a function of the application. That function determines that mouse was "on button". If we have registered functions that should be called, when "button is clicked", then those functions will be called. Within such function can be if statements to affect what is done, because user did "click the mouse".

... or that is how some GUI frameworks on some systems implement the workflow.
Last edited on
Topic archived. No new replies allowed.