C++ 关于public private,protect的疑问

public修饰的成员变量 
在程序的任何地方都可以被访问,就是公共变量的意思,不需要通过成员函数就可以由类的实例直接访问

private修饰的成员变量 
只有类内可直接访问,私有的,类的实例要通过成员函数才可以访问,这个可以起到信息隐藏

protected是受保护变量   
类内和子类可直接访问,也就是说,基类中有protected成员,子类继承于基类,那么也可以访问基类的protected成员,要是基类是private成员,则对于子类也是隐藏的,不可访问

posted @ 2011-07-06 14:10 Smart Pointer 阅读(349) | 评论 (0)编辑 收藏

类成员的显式初始化

Although most objects are initialized by running an appropriate constructor, it is possible to initialize the data members of simple nonabstract classes directly. Members of classes that define no constructors and all of whose data members are public may be initialized in the same way that we initialize array elements:

尽管大多数对象可以通过运行适当的构造函数进行初始化,但是直接初始化简单的非抽象类的数据成员仍是可能的。对于没有定义构造函数并且其全体数据成员均为 public 的类,可以采用与初始化数组元素相同的方式初始化其成员:

struct Data {
         
int ival;
         
char *ptr;
     };
     
// val1.ival = 0; val1.ptr = 0
     Data val1 = { 00 };

     
// val2.ival = 1024;
     
// val2.ptr = "Anna Livia Plurabelle"
     Data val2 = { 1024"Anna Livia Plurabelle" };

The initializers are used in the declaration order of the data members. The following, for example, is an error because ival is declared before ptr:

根据数据成员的声明次序来使用初始化式。例如,因为 ivalptr 之前声明,所以下面的用法是错误的:

   // error: can't use "Anna Livia Plurabelle" to initialize the int ival
     Data val2 = { "Anna Livia Plurabelle" , 1024 };

This form of initialization is inherited from C and is supported for compatibility with C programs. There are three significant drawbacks to explicitly initializing the members of an object of class type:

这种形式的初始化从 C 继承而来,支持与 C 程序兼容。显式初始化类类型对象的成员有三个重大的缺点。

  1. It requires that all the data members of the class be public.

    要求类的全体数据成员都是 public

  2. It puts the burden on the programmer to initialize every member of every object. Such initialization is tedious and error-prone because it is easy to forget an initializer or to supply an inappropriate initializer.

    将初始化每个对象的每个成员的负担放在程序员身上。这样的初始化是乏味且易于出错的,因为容易遗忘初始化式或提供不适当的初始化式。

  3. If a member is added or removed, all initializations have to be found and updated correctly.

    如果增加或删除一个成员,必须找到所有的初始化并正确更新。


It is almost always better to define and use constructors. When we provide a default constructor for the types we define, we allow the compiler to automatically run that constructor, ensuring that every class object is properly initialized prior to the first use of that object.

定义和使用构造函数几乎总是较好的。当我们为自己定义的类型提供一个默认构造函数时,允许编译器自动运行那个构造函数,以保证每个类对象在初次使用之前正确地初始化。

Exercises Section 12.4.5

The data members of pair are public, yet this code doesn't compile. Why?

pair 的数据成员为 public,然而下面这段代码却不能编译,为什么?

     pair<int, int> p2 = {0, 42}; // doesn't compile, why? 
解答:对于没有定义构造函数并且其全体数据成员均为 public 的类,可以采用与初始化数组元素相同的方式初始化其成员。

posted @ 2011-07-06 14:04 Smart Pointer 阅读(468) | 评论 (0)编辑 收藏

浅谈C++类(4)--隐式类类型转换

     摘要: 欢迎转载,但请标明作者 “九天雁翎”,当然,你给出这个帖子的链接更好。老规矩,看个例子,知道我要说的是什么。 例4.0: Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1#include <st...  阅读全文

posted @ 2011-07-05 20:32 Smart Pointer 阅读(325) | 评论 (0)编辑 收藏

一个无边框的Qt实例

实现一个以图片边框为窗口边框的Qt程序,可以鼠标左键拖动,点击右键可以推出程序。

本程序一共三个文件,分别为shapewidget.h,shapewidget.cpp,main.cpp

自定义头文件:shapewidget.h

#ifndef SHAPEWIDGET_H
#define SHAPEWIDGET_H

#include <QtGui>

class QPoint;       //定义一个Qpoint类

class ShapeWidget : public QWidget //ShaoeWidget继承了QWidget
{
    Q_OBJECT
public:
    ShapeWidget(QWidget *parent=0);        //创建一个空内容的构造函数

protected:
    void mousePressEvent(QMouseEvent *);        //自定义一个鼠标点击事件函数
    void mouseMoveEvent(QMouseEvent *);         //自定义一个鼠标拖动事件函数
    void paintEvent(QPaintEvent *);             //自定义一个刷屏事件函数

private:
    QPoint dragPosition;        //定义一个QPoint的成员变量

};

#endif

实现文件:shapewidget.cpp

#include "shapewidget.h"

ShapeWidget::ShapeWidget(QWidget *parent)   //外部重写构造函数
        : QWidget(parent,Qt::FramelessWindowHint)   //初始化参数类型
{
    QPixmap pix;    //设置一个QPixmap的对象。
    pix.load(":/images/Watermelon.png",0,Qt::AvoidDither|Qt::ThresholdDither|Qt::ThresholdAlphaDither);
    resize(pix.size()); //设置窗口的尺寸为图片的尺寸
    setMask(pix.mask());   //先通过pix的方法获得图片的过滤掉透明的部分得到的图片,作为shapeWidget的不规则边框。
}

void ShapeWidget::mousePressEvent(QMouseEvent * event)
{
    if (event->button() == Qt::LeftButton) //点击左边鼠标
    {
         dragPosition = event->globalPos() - frameGeometry().topLeft();
         //globalPos()获取根窗口的相对路径,frameGeometry().topLeft()获取主窗口左上角的位置
         event->accept();   //鼠标事件被系统接收
    }
    if (event->button() == Qt::RightButton) 
    {
         close();
    }
}

void ShapeWidget::mouseMoveEvent(QMouseEvent * event)
{
    if (event->buttons() == Qt::LeftButton) //当满足鼠标左键被点击时。
    {
         move(event->globalPos() - dragPosition);//移动窗口
         event->accept();
    }
}

void ShapeWidget::paintEvent(QPaintEvent *)
{
    QPainter painter(this);//创建一个QPainter对象
    painter.drawPixmap(0,0,QPixmap(":/images/Watermelon.png"));//绘制图片到窗口
    /*
      QPixmap(":/images/Watermelon.png")如果改为QPixmap(),则只能看到绘制出的框架,看不到图片颜色,也就是看不到图片。
    */
}

主函数:main.cpp

#include <QApplication>
#include "shapewidget.h"

int
main(int argc, char * argv[])
{
    QApplication app(argc,argv);
    ShapeWidget shape;
    shape.show();
    return app.exec();
}

原地址:http://linhui.568.blog.163.com/blog/static/962652682010789339688/

来自: http://hi.baidu.com/sd%B2%CB/blog/item/f5de253e6294dee03d6d975e.html

实现一个以图片边框为窗口边框的Qt程序,可以鼠标左键拖动,点击右键可以推出程序。

本程序一共三个文件,分别为shapewidget.h,shapewidget.cpp,main.cpp

自定义头文件:shapewidget.h

#ifndef SHAPEWIDGET_H
#define SHAPEWIDGET_H

#include <QtGui>

class QPoint;       //定义一个Qpoint类

class ShapeWidget : public QWidget //ShaoeWidget继承了QWidget
{
    Q_OBJECT
public:
    ShapeWidget(QWidget *parent=0);        //创建一个空内容的构造函数

protected:
    void mousePressEvent(QMouseEvent *);        //自定义一个鼠标点击事件函数
    void mouseMoveEvent(QMouseEvent *);         //自定义一个鼠标拖动事件函数
    void paintEvent(QPaintEvent *);             //自定义一个刷屏事件函数

private:
    QPoint dragPosition;        //定义一个QPoint的成员变量

};

#endif

实现文件:shapewidget.cpp

#include "shapewidget.h"

ShapeWidget::ShapeWidget(QWidget *parent)   //外部重写构造函数
        : QWidget(parent,Qt::FramelessWindowHint)   //初始化参数类型
{
    QPixmap pix;    //设置一个QPixmap的对象。
    pix.load(":/images/Watermelon.png",0,Qt::AvoidDither|Qt::ThresholdDither|Qt::ThresholdAlphaDither);
    resize(pix.size()); //设置窗口的尺寸为图片的尺寸
    setMask(pix.mask());   //先通过pix的方法获得图片的过滤掉透明的部分得到的图片,作为shapeWidget的不规则边框。
}

void ShapeWidget::mousePressEvent(QMouseEvent * event)
{
    if (event->button() == Qt::LeftButton) //点击左边鼠标
    {
         dragPosition = event->globalPos() - frameGeometry().topLeft();
         //globalPos()获取根窗口的相对路径,frameGeometry().topLeft()获取主窗口左上角的位置
         event->accept();   //鼠标事件被系统接收
    }
    if (event->button() == Qt::RightButton) 
    {
         close();
    }
}

void ShapeWidget::mouseMoveEvent(QMouseEvent * event)
{
    if (event->buttons() == Qt::LeftButton) //当满足鼠标左键被点击时。
    {
         move(event->globalPos() - dragPosition);//移动窗口
         event->accept();
    }
}

void ShapeWidget::paintEvent(QPaintEvent *)
{
    QPainter painter(this);//创建一个QPainter对象
    painter.drawPixmap(0,0,QPixmap(":/images/Watermelon.png"));//绘制图片到窗口
    /*
      QPixmap(":/images/Watermelon.png")如果改为QPixmap(),则只能看到绘制出的框架,看不到图片颜色,也就是看不到图片。
    */
}

主函数:main.cpp

#include <QApplication>
#include "shapewidget.h"

int
main(int argc, char * argv[])
{
    QApplication app(argc,argv);
    ShapeWidget shape;
    shape.show();
    return app.exec();
}

原地址:http://linhui.568.blog.163.com/blog/static/962652682010789339688/

posted @ 2011-06-26 11:03 Smart Pointer 阅读(2665) | 评论 (0)编辑 收藏

仅列出标题
共2页: 1 2 
<2011年11月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

导航

统计

常用链接

留言簿

随笔档案

收藏夹

搜索

最新评论

阅读排行榜

评论排行榜