具有空白/功能的类或结构? C ++ [英] Class or Struct with a void/function in them? C++

查看:62
本文介绍了具有空白/功能的类或结构? C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我是C ++的新手,我想知道我是否可以创建一个带有函数的struct / class来操作该类中的数据。例如,我有以下内容:



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
    {
    public:

        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;
            else
                return 0;
        }
    };
}





我试图从微软为一个小应用程序重新创建System :: IO :: Stream类我正在制作。我想将流设置为like并将readInt32等函数放入struct / class中。这可能吗?另外我如何将文件路径作为参数传递给类?



ie:

ByteReader :: IO br = new ByteReader :: IO(文件路径);



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?

ie:
ByteReader::IO br = new ByteReader::IO( filepath );

推荐答案

你必须使用所谓的构造函数来将某些内容传递给类。



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

class IO{
public:
  IO(const char* filePath){_filePath=filePath;};
private:
  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.


请看我对这个问题的评论。



只是为了给你和想法。静态方法就像旧的非类方法一样,只能访问类(其他类成员),但实例成员有隐藏参数this。因此,在这些方法中,您可以编写如下内容:

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;



这将使用method参数或其他任何内容修改实例的字段 myField 其他领域。



请看我过去的回答: Catch 22 - 当使用它们的函数变为静态时,指向接口对象的指针会死亡。 [ ^ ]。



你看,我只回答你的标题问题,如解决方案1.解决方案基本上是正确但不完整。你对重新创造的猜测是完全不清楚的;我怀疑他们是基于一些误解,但我无法弄清楚到底是什么,对不起。



-SA


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.

—SA


我想念你定义的结构点,但是,你可以这样做。我假设您将为类添加更多功能以使用它。



此外,在您的filesize()api中,参数filepath应为const char * filepath而不是char * filepath。
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".


这篇关于具有空白/功能的类或结构? C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆