Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

function calling in c++

Name: Anonymous 2007-01-10 20:23

I'm kinda new to c++, and I have a question that I can't really define well enough for google to give me answers.

basically, what i want to do is loop through a list of classes that i know have a specific function and call it. I've tried something like
   *(ObjectArray[0] + loop).function();
where ObjectArray would be an string array that holds all of the function names. This fails, because function() is not defined, even though I've declared it in the class.

Any ideas?

Name: Anonymous 2007-01-10 21:20

Let's see... First, to loop through the elements of an array, you would do ObjectArray[loop].  Second, you are taking the value in ObjectArray[0], adding loop to it, and dereferencing it like a pointer, which shouldn't even work unless ObjectArray is an array of pointers.  Besides that, you need to actually instantiate the class, trying to call function() on an array of strings will look for function() in the string libraries.

Name: Anonymous 2007-01-10 21:24


class SampleClass;
class SampleClass2 : public SampleClass { ... };
class SampleClass3 : public SampleClass { ... };

SampleClass* Array[2];
Array[0] = new SampleClass2;
Array[1] = new SampleClass3;

for(int i=0;i<2;i++) Array[i].function();


Note that the array is an array of base class pointers, and the other classes inherit from the base class.

Name: Anonymous 2007-01-11 0:30

wow, i feel like a /b/tard...
thanks a lot!

Name: Anonymous 2007-01-11 1:59

>>3
sorry to bother again, but it's having problems with instantiating the class...
Array[0] = new SampleClass2;
style coding is coming up with
"expected constructor, destructor, or type conversion before '=' token"
I'm using dev-cpp, btw if that matters
know what i'm doing wrong?

Name: Anonymous 2007-01-11 7:50

>>5
All your classes need constructors and destructors.

Name: Anonymous 2007-01-11 9:56


for my $class ("SampleClass1", "SampleClass2", "SampleClass3") {
      $class->new;
      $class->function;
}

Name: Anonymous 2009-01-14 15:11

NO U

Name: Anonymous 2009-02-25 7:45


open the file you   created and shit   that will basicly   use arrays loops   and conditional statements   So for the   standard spelling Perhaps.

Name: Trollbot9000 2009-07-01 8:42


Array because the code?

Name: Anonymous 2011-04-14 10:31

CALL MY ANUS

Name: Anonymous 2011-04-14 12:41

>>5
C++ is a statically typed language, it doesn't have first-class member function reflection or dynamic binding. Instead, you want to use member function polymorphism (virtual functions) and a class hierarchy as >>3 pointed out.

Secondly, don't use dev-cpp. Perhaps the book you started reading uses it, but dev-cpp hasn't been updated in years, its version of GCC it uses is ancient. It has known security problems. It generates slow code. No one in the software development industry uses it.

If you're on Windows, you should be using the free and excellent Microsoft Visual C++ 2010 Express Edition. I recommend staying away from MinGW and Cygwin, they kind of blow chunks on Windows.

If you're on Linux, you should be using a recent version GCC.

Name: Anonymous 2011-04-14 16:42

>>13
MinGW ≫ MSVC

Name: Anonymous 2011-04-15 11:23

>>13
If you're on Windows, you should be using the free and excellent Microsoft Visual C++ 2010 Express Edition.

If you're able to download it. Their fucking download site doesn't fucking work even with their own fucking browser.

Name: Sgt.Kabu遬إkiman꾛ᤂ 2012-05-28 21:48

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

Don't change these.
Name: Email:
Entire Thread Thread List