Click here to Skip to main content
11,412,111 members (66,658 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C++ function
Hello, I am fairly new to C++ and I was wondering if I can make a struct/class with a function in that to manipulate data within that class. For example I have the following:

#include <sys/stat.h>
#include <fstream>

namespace ByteReader
    public class IO
        struct Stream
            long length;
            long position;
            unsigned char* data;
        struct stat results;
        int fileSize(char* filepath)
            if (stat(filepath, &results) == 0)
                return results.st_size;
                return 0;

I was trying to recreate the System::IO::Stream class from microsoft for a little app I'm making. I would like to have the stream setup like and have functions such as ReadInt32 into the struct/class. Is that possible to do? Also how would I pass a filepath as an argument in the class?

ByteReader::IO br = new ByteReader::IO( filepath );
Posted 3-Jan-13 20:40pm
Sergey Alexandrovich Kryukov at 4-Jan-13 1:44am
I cannot see what's your problem. And there is no such thing as argument in the class. Functions can have argument, and you already use them.
jakes625 at 4-Jan-13 1:52am
@-SA I am trying to recreate a stream namespace/class for native C++. For example I want to do the following:

ByteReader::IO br = new ByteReader::IO( @"C:\Folder\Folder\file.log" );
int n = br->ReadInt32();
char* string = br->ReadChars( 8 );

n would return an int, string would return a char array read from that file.

Doesthis help any?
Sergey Alexandrovich Kryukov at 4-Jan-13 3:41am
What does it mean: "recreate a namespace"? "Recreate a class"? You are missing something really, really basic.
You should better explain your ultimate goal, without your idea of implementation.
Philip Stuyck at 4-Jan-13 5:44am
by creating a class, he means instantiating one. It would be better to use the correct names for things but for a beginner that is not easy. The amount of stuff to learn is huge. Nevertheless, if you want to create a instance of a class, this is called instantiating. What you get back is an object. An object is an instance of a class.
Sergey Alexandrovich Kryukov at 4-Jan-13 7:26am
I agree, but I still cannot understand those "recreating".
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You have to use what is called a constructor to 'pass something to the class'.

class IO{
  IO(const char* filePath){_filePath=filePath;};
  const char* _filePath;

The whole point of methods is to do manipulations on the class itself.
I think you should do some more reading. A constructor is really elementary stuff.
Sergey Alexandrovich Kryukov at 4-Jan-13 3:38am
Not necessarily. Any instance function can do that, because it has "this" parameter (hidden).
Philip Stuyck at 4-Jan-13 5:37am
I kept the explanation minimal for a novice user. There is also class methods, which I said nothing about.
Sergey Alexandrovich Kryukov at 4-Jan-13 7:24am
A constructor is also a method. If you simply did not even mention the word "constructor", it would be more correct. When it comes to the subject under discussion, constructors are nothing special.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Please see my comments to the question.

Just to give you and idea. Static methods are like old non-class methods, only have access to class (other class members), but instance members has hidden parameter "this". So, in such methods, you can write something like:
this->myField = myMethodParameter;
This will modify the instance's field myField using the method parameter, or anything else, say, some other field.

Please see my past answer: Catch 22 - Pointers to interface objects die when function using them is made static.[^].

You see, I just respond only to your question of the title, as in Solution 1. That solution is basically correct but incomplete. You speculations about "re-creation" are completely unclear; I suspect they are based on some misconception, but I cannot figure out what exactly, sorry.

Philip Stuyck at 4-Jan-13 5:41am
Maybe he is just asking if it is possible to add a method that returns void. And then the answer is yes of course.
Sergey Alexandrovich Kryukov at 4-Jan-13 7:25am
Maybe. Good point.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

I miss the point of the struct you defined, but yes, you can do that. I assume you will add more functionality to the class to use it.

Also, in your filesize() api, the parameter filepath should be "const char* filepath" and not "char* filepath".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 348
1 Sergey Alexandrovich Kryukov 225
2 Sascha Lefévre 150
3 ProgramFOX 130
4 Maciej Los 110
0 Sergey Alexandrovich Kryukov 8,955
1 OriginalGriff 7,158
2 Maciej Los 3,480
3 Abhinav S 3,248
4 Peter Leow 3,059

Advertise | Privacy | Mobile
Web03 | 2.8.150414.5 | Last Updated 4 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100