member-functions

What is better practice when programming a member function?

I have seen member functions programed both inside of the class they belong to and outside of the class with a function prototype inside of the class. I have only ever programmed using the first method, but was wondering if it is better practice to use the other or just personal preference? ...

mem_fun fails, pthread and class ptr

pthread takes in as its parameter void (start_routine)(void* userPtr), i was hoping i can use std::mem_fun to solve my problem but i cant. I would like to use the function void * threadFunc() and have the userPtr act as the class (userPtr->threadFunc()). Is there a function similar to std::mem_func that i can use? ...

Is it good form to compare against changing values in a loop in C++?

No doubt some of you have seen my recent posting, all regarding the same program. I keep running into problems with it. To reiterate: still learning, not very advanced, don't understand pointers very well, not taking a class, don't understand OOP concepts at all, etc. This code just merges two sorted vectors, farray and sarray, into a si...

Iterate over all members of a object within a function of that object

It would be exceedingly handy if I could do this: var MyObject = function(param1, param2, ... paramN) { this.var1 = stuff; this.var2 = moreStuff; . . . this.varN = nStuff; this.validate = function() { for(var current in this) { alert(current); //validate all member...

Why are some operators in C++ only allowed to be overloaded as member functions?

The operators are = () [] -> ->* conversion operators These can be declared only as member functions. Any other operator function can be either a class member or a non-member function. What is the rationale for this restriction? ...

Are preconditions and postconditions needed in addition to invariants in member functions if doing design by contract?

I understand that in the DbC method, preconditions and postconditions are attached to a function. What I'm wondering is if that applies to member functions as well. For instance, assuming I use invariants at the beginning at end of each public function, a member function will look like this: edit: (cleaned up my example) void Charcoa...

Determine whether a class has a function

Using a trick (described by Olivier Langlois), I can determine whether a class has a type defined: template<typename T> struct hasType { template<typename C> static char test( typename C::Type ); template<typename C> static char* test(...); enum{ Value= sizeof(test<T>(0))==1 }; }; I can also determine whether a class has a...

C++: Pointer to class member function inside a non-related structure

I've done a bit of reading online as to how to go about this and I think I'm doing it correctly... My goal is to have an array of structure objects that contain pointers to member-functions of a class. Here's what I have so far... typedef void (foo::*HandlerPtr)(...); class foo { public: void someFunc(...); // ... private: ...

Order of operator overload resolution involving temporaries

Consider the following minimal example: #include <iostream> using namespace std; class myostream : public ostream { public: myostream(ostream const &other) : ostream(other.rdbuf()) { } }; int main() { cout << "hello world" << endl; myostream s(cout); s << "hello world" << endl; myostr...

What's the default inheritant type of a member function in PHP?

class foo implements Countable { function count() { # do stuff here } } What's the type of count,public,protect or private? ...

Calling virtual functions inside member functions

I'm reading Thinking in C++ by Bruce Eckel. In Chapter 15 (Volume 1) under the heading "Behaviour of virtual functions inside constructor", he goes What happens if you’re inside a constructor and you call a virtual function? Inside an ordinary member function you can imagine what will happen – the virtual call is resolved a...

Importance of a singlecolon ":" in C++

Rarely in the regular codes I encounter the a single colon in classes for e.g.: A::member():b(),c() { } What is the importance of the single colon over here? Why is it used here? Is it mandatory sometimes? If so in which cases? ...

Is there a practical benefit to casting a NULL pointer to an object and calling one of its member functions?

Ok, so I know that technically this is undefined behavior, but nonetheless, I've seen this more than once in production code. And please correct me if I'm wrong, but I've also heard that some people use this "feature" as a somewhat legitimate substitute for a lacking aspect of the current C++ standard, namely, the inability to obtain the...

Class member functions instantiated by traits [policies, actually]

I am reluctant to say I can't figure this out, but I can't figure this out. I've googled and searched Stack Overflow, and come up empty. The abstract, and possibly overly vague form of the question is, how can I use the traits-pattern to instantiate member functions? [Update: I used the wrong term here. It should be "policies" rather ...

Const Functions and Interfaces in C++

I'll use the following (trivial) interface as an example: struct IObject { virtual ~IObject() {} virtual std::string GetName() const = 0; virtual void ChangeState() = 0; }; Logic dictates that GetName should be a const member function while ChangeState shouldn't. All code that I've seen so far doesn't follow this logic, though...

Number of args for stored procedure PLS-00306

Hi I have problem with calling for my procedure. Oracle scrams PLS-00306 Error: Wrong number of types of arguments in call to procedure. With my type declaration procedure has exact the same declaration like in header below. If I run it as separate procedure it works, when i work in ODCI interface for extensible index creation, i...

F#: any way to use member functions as unbound functions?

Is there a way to extract member functions, and use them as F# functions? I'd like to be able to write the following: mystring |> string.Split '\n' |> Array.filter (string.Length >> (=) 0 >> not) The code above works if you [let] let mystring = "a c\nb\n" let stringSplit (y:char) (x:string) = x.Split(y) let stringLength (x:string) = ...

I'm new to C++. Please Help me with the Linked List (What functions to add)?

DEAR All; Hi, I'm just beginner to C++; Please help me to understand: What functions should be in the Linked list class ? I think there should be overloaded operators << and >>; Please help me to improve the code (style, errors, etc,) Thanks for advance. Igal. Edit: This is only first sta...

What's the best way to sum the result of a member function for all elements in a container?

Let's say I have the following object: struct Foo { int size() { return 2; } }; What's the best way (most maintainable, readable, etc.) to get the total size of all objects in a vector<Foo>? I'll post my solution but I'm interested in better ideas. Update: So far we have: std::accumulate and a functor std::accumulate and a la...

Why can't I declare a friend in one class that is a private member of another class?

Given the following code: class Screen; class WindowMgr { WindowMgr& relocateScreen( int r, int c, Screen& s); }; class Screen { friend WindowMgr& WindowMgr::relocateScreen( int r, int c, Screen& s); // ^ cannot access private member declared in class 'WindowMgr' int m_nR, m_nC; }; WindowMgr& WindowMgr::reloc...