You are not logged in.
It is an array of 1 dimensional arrays, but in order to be able to create a data structures such as that, you need to define the structure. With an array, you tell the computer "give me an array of size 10". With a (dynamic) matrix, you can't just say "give me a matrix of size 5x4". This is because the memory itself is shaped like an array, but it's not shaped like a matrix. This makes creating arrays more natural than doing so with a matrix.
"In the real world, this would be a problem. But in mathematics, we can just define a place where this problem doesn't exist. So we'll go ahead and do that now..."
Offline
my latest topic: Pointers to functions!
I'm having some compiler/book disagreement when using pointers to member functions.
consider the following program
#include <iostream>
class Dog
{
public:
Dog() {}
~Dog() {}
// method to be pointed to
int bark(long l, double d) { std::cout << "bark " << (l*d) << " times\n"; return 0; }
};
int main()
{
// declare pointer to any dog method that takes a long and a double and returns an int
int (Dog::*p)(long, double);
p = Dog::bark; // bad???
Dog pokey;
(pokey.*p)(2,4);
return 0;
}
according to my book, the statement p = Dog::bark; is how you make p point to that method. On my compiler at home (borland) it compiles and works fine. But today I was using a compiler on a computer in my schools computer lab, and it complained about that statement as being an invalid use of a nonstatic method.
I googled some articles. Some of which said you have to use 'p = &Dog::bark;' but the compiler issued the same complaint.
Note the pointer declaration worked fine, but the compiler yelled everytime i tried to assign it to something. But again, both 'p = Dog::bark;' and "p = &Dog::bark;' compiled and ran on my home compiler.
So my question is, is the compiler at school behaving improperly, or is my current compiler outdated? IF so, what is the correct way to make p point to a member function?
Last edited by mikau (2008-02-06 17:23:52)
A logarithm is just a misspelled algorithm.
Offline
It's just a typo I believe.
#include <iostream>
class Dog
{
public:
Dog() {}
~Dog() {}
// method to be pointed to
int bark(long l, double d) { std::cout << "bark " << (l*d) << " times\n"; return 0; }
};
int main()
{
// declare pointer to any dog method that takes a long and a double and returns an int
int (Dog::*p)(long, double);
p = &Dog::bark; // bad???
Dog pokey;
(pokey.*p)(2,4);
return 0;
}
"In the real world, this would be a problem. But in mathematics, we can just define a place where this problem doesn't exist. So we'll go ahead and do that now..."
Offline
mm... i think you read my post too fast, Ricky! I said I tried that and it didn't work.
But at the moment I'm about to leave for school and haven't much time so.. perhaps I read YOUR post too fast. I'll look more carefully later.
Last edited by mikau (2008-02-07 01:57:18)
A logarithm is just a misspelled algorithm.
Offline
What compilers are you using? Need name and version numbers.
"In the real world, this would be a problem. But in mathematics, we can just define a place where this problem doesn't exist. So we'll go ahead and do that now..."
Offline
they don't have C++ compilers on my schools computers (my school is java biased!)
here's where i download it
http://www.bloodshed.net/devcpp.html
A logarithm is just a misspelled algorithm.
Offline
Bloodshed Dev-C++ is a full-featured Integrated Development Environment (IDE) for the C/C++ programming language. It uses Mingw port of GCC (GNU Compiler Collection) as it's compiler. Dev-C++ can also be used in combination with Cygwin or any other GCC based compiler.
If you don't know what this means here is a bit of an explanation. The compiler is a program which takes input files (and some addition parameters) and spits out some type of byte-code. Compilers, like any programs, must be run in some form. As with any program, it must be run. Since the compiler typically requires input (namely, code) it must be run from the command line.
That's all fine and dandy, but compilers being run from the command line can be typically hard to use. It is tedious to type in a line every time you want to compile. Also, there are many options which can make that line very long and confusing. In order to simplify this, a Integrated Developement Environment is used. All an IDE is, is a program which runs a compiler. As such, they are almost always not connected, but rather the IDE just generates that compile line for you. It would be rather idiotic for an IDE to not come with a text editor to allow you to modify your code, and some even have debugging tools.
So in the above quote, Bloodshed is just an IDE. It runs off the Mingw port of GCC. GCC, in turn, is the standard compiler used by Linux systems. I'd have to do some testing with Mingw, but it looks like if that was your code, then there is a pretty darn big bug in the compiler.
"In the real world, this would be a problem. But in mathematics, we can just define a place where this problem doesn't exist. So we'll go ahead and do that now..."
Offline
this is weird...
I clicked on help/about dev-C++ in the IDE, here's what it says at the top:
GNU GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (C) 1989, 1991 Free Software Foundation, Inc.
675 Mass Ave, Cambridge, MA 02139, USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
the license is apparently about 17 years old... but I seriously doubt they would put a 1991 compiler on a website still in use today. (or at least as of august 2007)
It may be just an outdated compiler, but even if it was, it still understands the declaration:
int (Dog*p)(long, double);
it just doesn't let me do anything with p after that. At least i haven't found a way yet.
anyway, Ricky. This isn't a major issue, first because i normally use my borland compiler, and second because i don't really intend to use pointers to functions anyway as it seems to be a discouraged technique. The point is that I know the correct way to do it when I need it, and to recognize when other people use it.
SO! which is the correct method? To use p = dog::bark; or p = &dog::bark; ?
A logarithm is just a misspelled algorithm.
Offline
& is proper, because you want that function's address.
pointers to functions anyway as it seems to be a discouraged technique
Not really, however it is fairly rare when they are used. See std::sort for a proper example.
the license is apparently about 17 years old...
Yes, however this does not mean that the compiler is. A license appears from the start date. So if I start a website in 2000 and keep it running till 2008, I would still put a copyright of 2000 on it.
"In the real world, this would be a problem. But in mathematics, we can just define a place where this problem doesn't exist. So we'll go ahead and do that now..."
Offline
thanks!
Btw, i had two queestions. Can you make a pointer to a method of class A point to a method of class B where B is a subclass of A? It failed to work when I tried it but I was wondering if maybe you could do it with casting.
Also, can you return a pointer to a function from a function? that is, can a functions return type be a pointer to a function?
A logarithm is just a misspelled algorithm.
Offline
Todays subject: internal and external linkage!
I'm just reading about namespaces in my C++ book for the first time. It began by introducing the subject of linkage, but its description is rather unclear.
here's my books description:
Linkage
Names can have internal and external linkage. These two terms refer to the use or availability of a name across multiple translation units or within a single translation unit. Any name having internal linkage can only be referred to within the translation unit in which it is defined. Names having external linkage are available to other translation units.
okay, but they don't explain what a 'translation unit' is. Immediatley after it uses the following listings to demonstrate internal and external linkage
first.cpp
int externalInt = 5;
const int j = 10;
int main()
{
return 0;
}
second.cpp
extern int externalInt;
int anExternalInt = 10;
const int j = 10;
after this it says the externalInt variable defined in line 1 of first.cpp has external linkage.
I don't follow. First, this is the first time i have ever seen the 'extern' keyword. So i'm not sure what the first line in second.cpp means, and how many instances of externalInt there are.
Usually my book explains things better than this. Can anyone explain to me whats happening between these two .cpp files?
Last edited by mikau (2008-02-13 19:49:56)
A logarithm is just a misspelled algorithm.
Offline
extern makes available the variable accross all cpp files.
although ive never seen it used in that sense, where its declared once in the primary file, and then declared external in another, it seems rather messy
The Beginning Of All Things To End.
The End Of All Things To Come.
Offline
Yeah, but in first.cpp it declares 'external int' without the 'extern' keyword. But, because of its name, I pressume it also is external.
So does the 'extern int externalInt' declaration in second.ccp create a new variable or does it refer to the same thing as the external int in first.cpp?
A logarithm is just a misspelled algorithm.
Offline