Excizted 67 Posting Whiz

Hi people. :)

I'm making a function callback system for a console, so I would dynamically be able to send a function pointer to my console class, and then it would be callable from the console.

Well, it works - but only with functions that are not member of a class.
I looked further into member function pointers, and I can't seem to figure a way to dynamically using them.

By dynamically, I mean any class. It seems to me, that I have to know the class name to make a typedef for the pointer.

And I wouldn't like to ruin my ability to have regular (non-member) function pointers too.


My typedef looks like this, now:

typedef void (*ConsoleFunction)(const std::string &);

And my function to add a pointer to the console is:

void Console::addItem(const std::string &name, void *point)
{
	ConsoleItem i;
	i.name = name;
	i.function = (ConsoleFunction) point;

	itemList.push_back(i);
}

The itemList is a simple STL list of ConsoleItem, and ConsoleItem is a struct containing name, pointer and a few other irrelevant variables.

So I'm wondering, how would I implement, so that I can use member functions with addItem aswell?

The classes will mostly be instanced, I don't know if it matters if its static or not, but if it matters, then the instanced class member pointers is the most important.

Hope to hear some advice (:

Thank you! :D

Excizted 67 Posting Whiz

Glad you got it working.
You're welcome (:

Excizted 67 Posting Whiz

Hehe, you're welcome :D

Excizted 67 Posting Whiz

Hi there :)

I happen to know that the sleep function has a capitalized S.
So you should call Sleep() instead of sleep().

The previous poster already told you what the compilation error means, remember that :)

Also Sleep is defined in the windows libraries, which you did include, so you should be good to go, if you just capitalize your S.

#include <windows.h>

Have fun :)

Excizted 67 Posting Whiz

I just want to know the HOST name of client machine. But i am only getting IP address of that machine by using getpeername function. I am new in SOCKET programming so please help if i am doing any mistake.

Example:
Suppose my machines host name is "Test". I can retrieve this by calling gethostname function. But what about host name of client machine which is recently get connected to server.

Aha.

Checking the official MSDN page for Winsock, I found gethostbyaddr which returns the name in a hostent structure.

http://msdn.microsoft.com/en-us/library/ms738521%28VS.85%29.aspx

http://msdn.microsoft.com/en-us/library/ms738552%28VS.85%29.aspx

If it does not give you the right name, you might just wanna use getHostName in the client app, and then sent the output back to the server?

Excizted 67 Posting Whiz

Hello there, i'm a new to this forum and i was always curious about game developing so here are some questions about game developing:

- Is it a good job?
- What qualities are required to develop a game?
- How much time does it take to develop a game?
- What is the most challenging aspect of this career or hobby?
- Is it fun for real or it's a very tough thing to do?
- What programming languages are the most redommended and why?

HUGE thanks in advance! :D
Happy new year

1) It's a very variating job, but it will mostly depend on the working environment, and how you like the others.

2) Enthutiasm, skills in your area.

3) A modern game takes more than a year to develop, starting from scratch, but mostly it depends on the amount of employees working on it, and having a good engine, or being able to reuse previous content is a huge time saver.

4) Well, the challenges would mainly be the work that you are not very good at, and need to learn about.

5) Is it mostly fun, but as many other things, game development has very boring work too, that you will pull your hair over, as documenting, or writing log messages, or debugging issues.

6) C++ is a great language, that's very flexible and powerful. Most games are build in C++, fx all Source games.

Excizted 67 Posting Whiz

Use code tags, man! :(

Excizted 67 Posting Whiz

aha, ok.

:D

Excizted 67 Posting Whiz

You're welcome :D

Excizted 67 Posting Whiz

You have to include the library, which is also output when you build DLL.
That library isn't a static library, but it just tells your project what content there is in the DLL.

Excizted 67 Posting Whiz

As earlier, it would have been better if you had posted the code that gave you the error. Somehow the descriptions don't really make the picture very clear !!

Well I did include the code...

class Foo
{
public:
	static void bar()
	{
		cout << "Foobar!\n";
	}
};
int main()
{

	//typedef void (*command)();
        typedef void (*command)(Manager*);
	typedef map<string, command> maptype;

	maptype cmdmap;

	cmdmap.insert(make_pair("bar", &Foo::bar));

	cmdmap.find("bar")->second();

}
Excizted 67 Posting Whiz

Static libraries are not DLL's.

DLL's are Dynamic Libraries, which is more the opposite of static libraries.

So if you're linking a static library, and then thinking "woot now it's collecting data from my DLL", then you're wrong. Try deleting the DLL, your program won't complain that it is missing a DLL :)

Just fyi.

Excizted 67 Posting Whiz

I have no idea why I would make a const object, but I'll take your advice and start making member functions

that don't modify an object's state

constants :)

Thanks!

Excizted 67 Posting Whiz

Well I would suggest you post the code which is giving the error, may be a pruned version with only the required fns and classes so we can exactly see what's happening. That's always much better.
normally declaring a function pointer to static member function does not require the class name to be put before the function pointer. It is declared just like a non-class function pointer, while assigning the address of the function we need the class name.

example: say i have a class 'A' with a static function 'void func(double)'

void (*pf)(double) = &A::func;

Thanks.

Using your advice I managed to get it working, or almost.

This is my code:

class Foo
{
public:
	static void bar()
	{
		cout << "Foobar!\n";
	}
};
int main()
{

	typedef void (*command)();
	typedef map<string, command> maptype;

	maptype cmdmap;

	cmdmap.insert(make_pair("bar", &Foo::bar));

	cmdmap.find("bar")->second();

}

Output: Foobar!

Then I tried adding my Manager pointer to the argument list like this:

typedef void (*command)(Manager*);

But I cannot compile - command is no longer the "key" (I have no clue what it is called).

The compiler says this:

error C2440: 'initializing' : cannot convert from 'void (__cdecl *)(void)' to 'command '

It works with a string, or aswell the Manager without the pointer-*.

Help is appreciated :)

Excizted 67 Posting Whiz

Yes, what I want is to make a pointer to a static class.
The code posted was my current code, with no class.

When making a class, and declaring the functions as static members therein, and adding ClassName:: in front of *Command, I got a compiler error, saying that my function does not take 2 arguments, on line 4 (second syntax in post #1).

Excizted 67 Posting Whiz

I bit the dust an created an instance of my class, making my functions non-static after finding this thread, using the advice given in post #4.

http://www.daniweb.com/forums/thread201865.html

Excizted 67 Posting Whiz

Hi all :)

Currently I have a console for my program, which in short words, executes the function named what you write.

The thing is, the functions needs, for several reasons, to be members of a class.

Currently I have the code below. Manager is a class that needs to travel with the command execution.

typedef void (*Command)(std::string arg, Manager *root);
typedef std::map<std::string, Command> MapType;
MapType commands;
MapType::const_iterator commandPos;

commands.insert(std::make_pair("testcommand", testcommand));

void testcommand(std::string arg, Manager *root)
{
/* do something */
}

Then to execute depending on string I'd do this:

std::string execute = "testcommand";
std::string arguments = "helloworld";
commandPos = commands.find(execute);
(*commandPos).second(arguments, m_rootmanager); // m_rootmanager is my pointer to my Manager class

I've tried in various ways to change my code to call a static function of a class, but I get all sorts of errors.

I tried googling, but the results all seemed far away from my current code, I would like not having to do it in a whole other way than I am already.

Help appreciated :D

Happy new year, y'all :P

Excizted 67 Posting Whiz

I can't believe you're asking here, I googled "resize image c++" and had atleast two good results.

http://tinyurl.com/yckgwaf

Have fun.

Excizted 67 Posting Whiz

I tried compiling and debugging your code.

On line 115 you return the variable parent which is not initialised, meaning you haven't set a value for it. When declaring it on line 99, you might wan't to NULL initialise it, saying treeq *parent = NULL;

I did that and next problem occurs on line 120 where you check

if root->leftch != NULL)

But what you don't consider, is that root is NULL, so it when you ask for root's member.

After changing it to this on line 120:

if(root != NULL) if(root->leftch != NULL){

There occured no more run time errors, and a blank console showed up.

Hope this helps you on with your code :)

Happy new year :P

Excizted 67 Posting Whiz

Well of course you can, you just have to link a C/C++ library for the codec you want to use :)

Excizted 67 Posting Whiz

I don't fully get what you want to know?
You want to get peer name, and right below you post the function to get peer name? :)

Excizted 67 Posting Whiz

Glad to help :)
Happy new year :D

Excizted 67 Posting Whiz

Correct me if I'm wrong...

So since the params in the constructor are only used to initialise the name and age variables, declaring them as const/reference is more a safety/performance advantage as opposed to any difference in functionality?

("difference in functionality" - for example, if name and age were declared const, they would not be able to be mutated later on in the program)

Indeed that is correct.

When you then copy the data from the const reference parameters, it will still be the same data, and the type of the new variable you create is not affected.

To summarize, not using reference would mean something like this:

std::string name = "something"; // < A string is created and is now taking up memory
classe(name); // < classe(string inputName); is making a new string with the same content as name.
classe(string inputName) : classname(inputName){} // < finally, the copied string is now copied once again

This is odd, to make 3 strings, for the same data, when you only need two, maybe even just one.
So using references is the way to do it to win performance.

Maybe you later add functionality to the ctor, and you might risk changing inputName, which is a reference to the original string. So you change the original string, which may be used by several other functions too *gasp*. So by making it const, you make sure you are unable to do that.

Excizted 67 Posting Whiz

Ofcourse you know how much it can compute in a second.. Just find the amound of hertz.

Excizted 67 Posting Whiz

Hi guys,

I understand the difference is that the params of ctor 2 are declared const with n being passed by reference.

However, I don't seem to understand what difference having these params (n and a) declared constant with n being passed by reference makes?

I mean, I'd understand the effect if the private members of 'name' and 'age' were declared constant, just not why n/a are?

Person(string n, int a) : name(n), age(a) {} // ctor 1

Person(const string &n, const int a) : name(n), age(a) {} // ctor 2

By passing a const reference, you make a read-only reference. By making a reference you don't copy the data in the variable, so you save memory, and you don't risk to change the content of the original variable, since you make it read only.

Excizted 67 Posting Whiz

Thank you :)

Excizted 67 Posting Whiz

I've never read that in a book, honestly, I don't have any consideral C++ books. But setters and getters works for me, and since I mostly make libraries used by others, it's the effects of setters and getters are highly useful!

Excizted 67 Posting Whiz

Well it's the other way around.
VERSION is hex, we agreed?
I wan't that number 10102 into an string, or integer :P

Excizted 67 Posting Whiz
int n = 15;
cout << hex;
cout << n << endl;

>> Well that's simple thanks, tho was more wondering how to convert it to a string?

You can use the stringstream.

#include<iostream>
#include<string>
#include<sstream>

using namespace std;

int main()
{
    const float PI = 3.14159265f;
    string  pie = "";
    stringstream convert;  //create stream object
    convert << PI;    //insert out value into our stream
    convert >> pie; //get the value in the stream as a string object

   return 0;
}

Or for practice, try to create a function that does that without the sstream.

But that's what I already did and got a string containing 65794.

Well almost, but isn't it the same as using my conversion function:

#include <sstream>
#include <string>

std::istringstream stream;
stream << VERSION;

std::string result = stream.str();
Excizted 67 Posting Whiz

I can only advise you to use setters and getters. It gives an overall more stability, atleast you will feel like it when you are programming within your own frameworks. It's hard to explain, but I'm sure you'll one day find out :D
And then it would be good if you are prepared and already have them implemented, instead of having to change a lot of code to have it. Well :)

int getx() const { return x; }

Hey Narue :), I'm just wondering, what difference does your example there have to this, without const?

int getx(){ return x; }

What happens when those brainless getters and setter, need to serve some purpose later one the code, or when the maintainer needs to change some code, or if the programmer makes some mistakes?

I agree! :P

Excizted 67 Posting Whiz
int n = 15;
cout << hex;
cout << n << endl;

Well that's simple thanks, tho was more wondering how to convert it to a string? 8) <- puppy eyes :D

Excizted 67 Posting Whiz

If you printed it as hex ("%x"), you'd see 10102, which contains ALL the numbers defined as VERSION_***.

<< is a shift operator. A major is 1 shifted left by 16 bits, a minor is 1 shifted left by 8 bits, a patch not shifted at all. Now they can bi combined safely into the single value, because they occupy different bit positions there.

I don't know much about bit manipulation yet, but I know that in this context the tokens/operators << and | are "bitwise operators". operator<< is the "left shift" operator. If x and y are 16-bit integers and x==0000000000000001 (which is 16-bit binary for the decimal value 1) and y==0000000000000000 (decimal 0), when you enter y = x << 8 you are telling the program to shift all the bits in the value x to the left 8 positions and store the new value to y. After this operation, y becomes 0000000100000000 (16-bit binary for the decimal number 256). operator | is the "bitwise inclusive OR" operator. It combines the bits of multiple values into one value. Continuing from the previous example:
Define a new 16-bit integer, z=0000000000000000 (decimal 0). Now, z = (x | y) will produce the result z=0000000100000001 (decimal 257).

In your question, VERSION is a macro that you can put in your code in place of the long string of shifts and ORs to represent the version information.

I don't know much beyond that though. I can't help you with the display …

Excizted 67 Posting Whiz

If you printed it as hex ("%x"), you'd see 10102, which contains ALL the numbers defined as VERSION_***.

<< is a shift operator. A major is 1 shifted left by 16 bits, a minor is 1 shifted left by 8 bits, a patch not shifted at all. Now they can bi combined safely into the single value, because they occupy different bit positions there.

Ahh great ;D I understand now :P

Except, if I may ask, how do I print them as hex, say we use the same VERSION #define?

Excizted 67 Posting Whiz

Heyloo ^_^

I've stumbled upon this code :D

#define VERSION_MAJOR 1
#define VERSION_MINOR 1
#define VERSION_PATCH 2

#define VERSION ((VERSION_MAJOR << 16) | (VERSION_MINOR << 8) | VERSION_PATCH)

I've been searching for some time now, unable to understand what << 16 and << 8 does, and why.

I converted VERSION to string, handling it as an integer, and printed out the result being 65794, which doesn't contain ANY of the numbers defined as VERSION_***.

Please help me with this wondering :)

Ps. Sorry for lousy thread title, I was unable to come up with a proper title :i

Thanks ;)

Excizted 67 Posting Whiz

Hello.

I'm wondering if I'm duplicating this class by doing the following.

SimpleClass *point;
SimpleClass sc;
sc.somevalue = something;
point = new SimpleClass(sc);

This post is quite tiny, I'm not being lazy, but I couldn't think of anything else needed for this question.

Thanks :)

Excizted 67 Posting Whiz

Because the filenames are the same, VC++ can't guess which way to handle it. No loop tho :)

Excizted 67 Posting Whiz

I have learned c++ and I want now to create an aplication but using microsoft visual c++ is very expensive because buying a licence for it not very cheap. That's why I was thinking about an open source.

I'm not sure what you're saying, but just for your interest; Visual C++ Express Edition is completely free and may be used for commercial usages, and it's got what you need. Well atleast I think so, I've not noticed anything missing, using it myself.

Excizted 67 Posting Whiz
#include <windows.h>

int main()
{
   HWND hWnd = GetConsoleWindow();
   ShowWindow(hWnd,SW_HIDE);
}

Wow I had no idea that was so easy! I might actually have use of that too, thanks ;)

Excizted 67 Posting Whiz

Hello I am rather new in visual c++ and I learned to code but I didn't find in any book how to ad an interface to my program. So please tell give me some references where from I could learn how to add a proper interface in visual c++.
Thanks

I guess you want to make proper Windows applications. Well the Win API is not easy to understand.

You should train a lot in console applications first.

Making graphical applications isn't just a matter of

make_graphical_application(true);

but you first of all need to design some graphic and work with absolute positions of where you want to post stuff.

A good place to start is in Visual C++: New Project > Win32 > Win32 Forms Application.

In here you from start have a windows application, and you are so lucky to have a toolbox to create common windows content. Don't get used to it tho, that's mostly not how it works in other projects.

Excizted 67 Posting Whiz

Well, I never worked with C++ because I had to.

I always had programming as an interest, and by keeping making small applications which interests you, you soon reach the stage where you make giantic projects.

You could make

  • Program to spy on your sis
  • A notepad app for yourself
  • A leet password system where you have to type whats under your pillow
  • Improved notepad which features your previous leet password system
  • A cool and smart digital diary with 256-bit encrypted password for your sister..
  • The same diary as above, but this version sends you email with new diary pages for laughs
  • Something that beats MSN Messenger, for you and your friends to chat over (this one gets fun if you implement admin powers :D)

Well, just make what you think could be funny, but funny does not always equal being useable or making your millionary.

Excizted 67 Posting Whiz

Well I've had this kind of behavior with #include.

If I in file 1 include file 2, and in file 2 include file 1, the both files would simply seem not to be there at compile time.

I guess it's the same for you. You told it to use a library, which you made two available of, then VC++ says "Hello Trashcan".

You should instead have a configuration for x86 and one for x64, and then include the respective folders in their configuration. Then prior to compilation, you choose which configuration to use, and VS will take care of including the right libraries.

Excizted 67 Posting Whiz

but how does the below program works with the * and &???

In the code below, the first line, which is a function declaration, the parameters are pointers, which is indicated with the * symbol.
By typing a * between the class and its instance-name, you create a pointer. A pointer simply enough points to a variable. So instead of having an int with it contents, you point to an other int, and if that other int changes, then you can read that through the pointer aswell. Pointers can be changed to point at something else, while references cant.

The second line we create a reference to our variables and send them into the function.
A reference means the actual path to whereever in the memory your integer (or other class) is located.
When defining a pointer, you give it a reference to whatever it must point to.

void getData (int *x,int *y,int *z);
getData (&x,&y,&z);

In this code, we have to put a * in front of the pointers. That is to dereference them. Because a pointer does not exactly equal what it is pointing to. If you printed x, y and z directly you would just get the "location-id" of the referenced item.

So putting the * in front of the pointer, dereferences them, so we get the content they are pointing to instead.

cin>>*x>>*y>>*z;

I hope you understand this.

Pointers and references are powerful and a large player in …

Excizted 67 Posting Whiz

@Excizted: What gave you the idea that this is C instead of C++?

This did:

Hai,
This is a source code written in turbo c for car racing game as a part of my higher secondary project.
1.Did I use the real method to constitute motion in c?
2.How can I include sound in c?

Excizted 67 Posting Whiz

Depends what you mean by align..

Excizted 67 Posting Whiz

Nice code, can be used for something, surely :)

Excizted 67 Posting Whiz

You haven't posted your constructor definition?

Excizted 67 Posting Whiz

Thanks!
Was getting scared no one could help me :D

Excizted 67 Posting Whiz

Hi everyone...

I have a silly problem.. Somehow I managed to faceroll or something, hitting a shortcut which shows all indents as arrows and all spaces as dots.

Here is a picture of the problem:

http://img97.imageshack.us/img97/8789/problemyf.jpg

I've looked the options through, but since I don't know what this thing I turned on is called, I could'nt find anything.

So if anyone know where to turn it off, or the hotkey, please come forward :)

Thanks! ;)

Excizted 67 Posting Whiz

Hai,
This is a source code written in turbo c for car racing game as a part of my higher secondary project.
May I get some advices to improve it?
1.Did I use the real method to constitute motion in c?
2.How can I include sound in c?
{ Function sound() is not working .. I think it is due to absence of speaker in my cabinet.. Can I use other sound functions.}
3.Which is the latest and simple compact C\C++ compailor?
4.Now what is the commercial use of C\C++ language?
5.Which compailor can I use in free Operating systems such as ubuntu,kubuntu, linux?

This is the C++ forum your're posting in, but you're asking about your C code?

Real method? In programming, I don't believe theres anything real and fake, basicly your program does what it's asked to.

compailor? compiler? Windows: Visual C++ Express 2008, Linux: MinGW.

Commercial use? You may use C++ for commercial purposes, if that's what you're asking.

MinGW I believe. I usually just use the make command, with g++.

Excizted 67 Posting Whiz

Ahh thanks, useful info there - also always wondered what was ment with Derived :)