After a long time not using C++, I picked it for a pet-project...
It works perfectly, but I have the feeling I use older solutions in my code, than those are available today. My code probably is pre C++11 (do we call it ANSI?)...
I'm looking for some reference material that can help me to learn the new and shiny things I may want to use in my code to make it faster, maintainable and readable...
“Real stupidity beats artificial intelligence every time.”
― Terry Pratchett, Hogfather
How do you use a pointer to a function that is declared in the general scope within a class.
Do you pass the pointer as parameter to class function, save the pointer as a class member and then use the class member in a class function when needed?
Usually I don`t try to compile wild guesses. I did things as you would do with a pointer in my post above but often in c++ things that resemble in some places don`t have a syntax that matches everywhere.
Well, you should because:
1. Might take less time than waiting for an answer.
2. Compiler is never cranky or in a bad mood. At most it will issue an error message.
3. One wild guess leads to another and soon you end up with a nice brilliant idea
Mircea is quite correct, you should always try and build small samples. Apart from anything else, the error reports help you to learn. The compiler, linker and debugger excellent tools that aid in development. Had you started in the days when we had to submit a deck of punched cards and wait 24 hours for the compiler output to tell us one character was mistyped, you would appreciate how easy things are these days.
It`s interesting to hear where things started. I had my first computer experience in the 80`s on a computer with keyboard and a dedicated green and white screen (the screen was displaying only two colors). My first programming experience was on a computer that you had to connect to a TV set. It was a computer with 16 colors graphics.
Mine was the LEO* III in the 60s, second row first three pictures at Leo Computers Society. Leo 3 photos[^]. Most input was punched paper tape, and some punched cards, no mass storage, only magnetic tape.
*The Lyons Electronic Office, initially developed between Lyons Catering and English Electric, both companies long gone.
You're creating a pointer to a free function (one defined outside a class) and then storing it, and invoking it, from within an object. But it's also possible to create a pointer to class member data or a member function. See here[^]. You may need to read several articles about this to gain a good understanding, because I can't point to one that is really good on its own. Search on "C++ pointer to member" and read articles that discuss the type Class::* (a pointer to a class member) and operators .* and ->*. These make the following possible:
int Class::* pm = &Class::m; // pointer to member data "int m"
int (Class::* pf)(int) = &Class::f; // pointer to member function "int f(int)"
Class c, *k;
c.*pm = 1; // c.m = 1;
n = c.*pf(0); // n = c.f(0);
k->.pm = 2; // k->m = 2;
n = (k->*pf)(0); // n = k->f(0); note that ->* requires parentheses
You don’t say exactly why it doesn’t work so I’ll just guess: the code works only with numbers up to 2^5=32. If you tried it with 192 it would have failed.
As a general advice, for small algorithms like that, it helps to “play the computer”: take a piece of paper and go through each step as you would be the computer. That gives you a better understanding of how the algorithm works and helps you find eventual bugs.
Last Visit: 31-Dec-99 18:00 Last Update: 3-Oct-23 1:40