小编For*_*ent的帖子

如何防止在非const对象上意外调用变异函数?

假设我们有一个类型为myType的Object obj,我们希望将它传递给函数Foo,它返回一些关于obj的有价值的信息.函数Bar是声明obj的地方,并且从中调用Foo,如下所示:

void Bar ()
{

myType obj; //default constructor

string valuableInfo = Foo(obj); 

//do other stuff
//...

} //end of Bar()
Run Code Online (Sandbox Code Playgroud)

这段代码当然没有说明Foo是将obj作为引用还是作为值,以及Foo是否以任何方式修改obj.

当然,如果Foo将obj作为值或const引用,我们就不会有任何问题.

string Foo (const myType & input); //this is fine 
string Foo (myType input); //so is this
Run Code Online (Sandbox Code Playgroud)

但我们不保证这个!函数签名很可能是

string Foo (myType & input); //asking for trouble!!
Run Code Online (Sandbox Code Playgroud)

但是检查我们想要传递obj的每个函数的签名是非常不方便的,那么我们如何指定我们只想将我们的对象传递给承诺不修改它的函数呢?

当然,一种方法是将obj声明为const,但这种方法的问题在于我们失去了灵活性.如果我们想在调用Foo(obj)后修改Bar()中的obj怎么办?

void Bar ()
{

const myType obj; //default constructor

string valuableInfo = Foo(obj); //compiler will complain if Foo's signature doesnt match

//we can't modify obj here :(
//...

} …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

26
推荐指数
5
解决办法
1136
查看次数

实用程序类的静态与非静态方法的性能

我有一个实用程序类,它具有非静态方法,没有实例变量.所以我在考虑将所有方法转换为static方法.我怀疑会有任何记忆或性能影响.但我只是想确认一下.

改变这样的方法static是否会对程序产生任何性能影响?

java performance static

14
推荐指数
2
解决办法
1万
查看次数

Java最佳实践:转换对象与接口

假设我们有以下玩具界面:

interface Speakable
{
    public abstract void Speak();
}

interface Flyer
{
    public abstract void Fly();
}
Run Code Online (Sandbox Code Playgroud)

我们有一个实现两个接口的类:

class Duck implements Speakable, Flyer
{
    public void Speak()
    {
        System.out.println("quack quack don't eat me I taste bad.");
    }

    public void Fly()
    {
        System.out.println("I am flying");
    }
}
Run Code Online (Sandbox Code Playgroud)

在这一点上,我看到了不同的方法来调用方法Duck,我无法决定哪一个是最佳实践.考虑这种情况:

public class Lab 
{
        private static void DangerousSpeakAndFly(Object x)
        {
            Speakable temp  = (Speakable) x;
            temp.Speak();
            Flyer temp2= (Flyer) x;
            temp2.Fly();
        }

        public static void main(String[] args) 
        {
            Duck daffy= new …
Run Code Online (Sandbox Code Playgroud)

java oop inheritance casting downcast

7
推荐指数
2
解决办法
1976
查看次数

分配给rvalue:为什么要编译?

在以下示例中:

class A {
  private: double content;

  public:
  A():content(0) {}

  A operator+(const A& other) {     
    content += other.content;
    return *this;
  }

 void operator=(const A& other) {
       content = other.content;
  }

};
Run Code Online (Sandbox Code Playgroud)

A是一个double的简单包装器,+并且=运算符已经过载.在以下用途中:

 int main(int argc, char *argv[]) {
    A a, b, c;
    (a+b) = c ; // Why is this operation legal?
}
Run Code Online (Sandbox Code Playgroud)

为什么(a+b) = c编译?我想知道为什么这个陈述是合法的,因为结果(a+b)必须是一个rvalue.我没有从中返回参考operator+.

c++ reference rvalue lvalue

5
推荐指数
1
解决办法
656
查看次数

这是一个错字还是我错过了什么?

美好的一天,我正在讨论Bjarne Stroustrup的"C++编程语言",我正面临一段代码,我认为这些代码应该是非法的,但是在文中提出.

我想知道这是否只是一个轻微的疏忽,或者是否有一些我遗漏的东西.

从第3章第63页开始:我们有用户定义的类型Vector,如下所示:

class Vector {
    private:
    double* elem; // elem points to an array of sz doubles
    int sz;

    public:
    Vector(int s) :elem{new double[s]}, sz{s} // constructor: acquire resources
    {
            for (int i=0; i!=s; ++i) elem[i]=0; // initialize elements
    }

    Vector(std::initializer_list<double>)
    {
    // initialize with a list
    } 
    // ...
    void push_back(double)
    { 
    // add element at end increasing the size by one
    }

    ~Vector() {
     delete[] elem; 
     } // destructor: release resources

    double& operator[](int i);

    int size() …
Run Code Online (Sandbox Code Playgroud)

c++ constructor c++11

4
推荐指数
1
解决办法
262
查看次数

将stax XML写入String

我正在使用stax来创建我的Web应用程序所需的XML文档.目前我在这样的文件中创建我的XML:

    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    String output=null;
     try 
     {
             XMLStreamWriter writer = factory.createXMLStreamWriter(
                     new FileWriter("C:\\Junk\\xmlDoc.xml"));
             writer.writeStartDocument();
             writer.writeStartElement("TagName1");
             writer.writeAttribute("AAA", "BBB");
             writer.writeEndElement();
             writer.writeEndDocument();             
             writer.flush();
             writer.close();
     } 
     catch (XMLStreamException e) 
     {
         e.printStackTrace();
     } 
     catch (IOException e) 
     {      
        e.printStackTrace();
     } 
Run Code Online (Sandbox Code Playgroud)

但是xml文件不是我想要的,我需要在一个中创建我的XML String.不幸的是我无法弄清楚OutputStream我需要哪个对象而不是FileWriter

java xml stax java-io

4
推荐指数
1
解决办法
3465
查看次数

C#在特定索引处为List <string>赋值

让我们考虑这段C#代码:

     List<string> a = new List<string>();
     a.Add("word1");
     a.Add("word2");
Run Code Online (Sandbox Code Playgroud)

现在让我说我想要"word3"而不是word2.我可以说

     a[1]="word2";
Run Code Online (Sandbox Code Playgroud)

但如果我试试

     a.ElementAt(2) ="word2"; 
Run Code Online (Sandbox Code Playgroud)

然后我得到一个错误,说左侧必须是一个变量.

对于引用类型和值类型,此语法的行为方式相同.谁能解释为什么第二种语法有缺陷?

c#

0
推荐指数
1
解决办法
2万
查看次数

标签 统计

c++ ×3

java ×3

c++11 ×2

c# ×1

casting ×1

constructor ×1

downcast ×1

inheritance ×1

java-io ×1

lvalue ×1

oop ×1

performance ×1

reference ×1

rvalue ×1

static ×1

stax ×1

xml ×1