class와 struct,, 간단한 용법에 관하여 질문드립니다!

junhongs의 이미지

class CDev : public Device
{
public:
CDev(const char *name, const char *devname, int irq = 0);
virtual ~CDev();
virtual int init();
virtual ssize_t read(struct file *filp, char *buffer, size_t buflen);
virtual off_t seek(struct file *filp, off_t offset, int whence);
virtual int ioctl(struct file *filp, int cmd, unsigned long arg);
....

protected:
static const struct file_operations fops;
...

}

위와같은 클래스가 존재하고

.
.
.
.

static int cdev_open(struct file *filp);
static int cdev_close(struct file *filp);
static ssize_t cdev_read(struct file *filp, char *buffer, size_t buflen);
static ssize_t cdev_write(struct file *filp, const char *buffer, size_t buflen);
static off_t cdev_seek(struct file *filp, off_t offset, int whence);
static int cdev_ioctl(struct file *filp, int cmd, unsigned long arg);
static int cdev_poll(struct file *filp, struct pollfd *fds, bool setup);

위와같은 일반 함수가 있습니다.

.
.
.
.

const struct file_operations CDev::fops = {
open : cdev_open,
close : cdev_close,
read : cdev_read,
write : cdev_write,
seek : cdev_seek,
ioctl : cdev_ioctl,
poll : cdev_poll,
};
동일 경로내에 fops 구조체를 정의하였습니다.

.
.
.
.

struct file_operations
{
int (*open)(FAR struct file *filp);
int (*close)(FAR struct file *filp);
ssize_t (*read)(FAR struct file *filp, FAR char *buffer, size_t buflen);
ssize_t (*write)(FAR struct file *filp, FAR const char *buffer, size_t buflen);
off_t (*seek)(FAR struct file *filp, off_t offset, int whence);
int (*ioctl)(FAR struct file *filp, int cmd, unsigned long arg);
};
구조체의 선언은 이렇습니다.

.
.
.
.

여기서 struct 내에 멤버함수포인터 뒤에 " : " 로 사용하였는데, 이 용법에 대해 궁금합니다.

용어를 모르니 검색해보아도 나오지도 않고 " : "는 주로 클래스 상속에 사용되는데 클래스의 멤버함수를 일반 멤버함수가 상속한다는건 있을 수도 없는 일 같습니다.
내용으로 보아 CDev::open 멤버함수와 cdev_open함수는 서로 연결고리가 있는것 같은데, 어떤 식으로 이루어진 것인지 궁금합니다!

질문을 위해 필요한 정도만 내용에 넣었습니다.. 감사합니다!

익명 사용자의 이미지

Designated Initializer라는 겁니다. 근데 표준 방식은 아니네요.

http://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html

저렇게 쓰는건 gcc의 확장인 듯 합니다. 근데 요샌 obsolete인 모양입니다.

C99 에서는 다음과 같이 쓰는데 이건 C++98에는 들어가 있지 않고, (이건 확인 안해봤지만)C++11에서도 안들어가 있는 모양이네요.

struct file_operations fops = {
   .read = device_read,
   .write = device_write,
   .open = device_open,
   .release = device_release
};

junhongs의 이미지

예제로 써주신 문법도 궁금했던 내용이었는데 둘다 해소해주셨네요.

답변 감사드립니다!

현재 저는 nuttx라는 cortex M4에 포팅되어있는 RTOS분석중이고 나온지 꽤 오래되어 그런것같습니다. 소스해석에 매진할 수 있겠네요..

익명 사용자의 이미지

오랫동안 답변을 써 왔는데
이렇게 정중한 감사인사 듣기는 오랫만이네요.

님같은 분에게 도움이 되어서 기쁩니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.